-
1.凡是广播必须是要到AndroidMainfest中配置信息的
<receiver android:name=".CallReceive"> -->类的全路径
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/> -->设置的是接收什么样的广播,根据实现的功能而定
</intent-filter>
</receiver>
-
-
-
- public class AppStatusReceiver extends BroadcastReceiver {
- /**
- * 在AndroidMainfest中添加:
- * <receiver android:name=".AppStatusReceiver" >
- <intent-filter>
- <action android:name="android.intent.action.PACKAGE_ADDED" />
- <action android:name="android.intent.action.PACKAGE_REPLACED" />
- <action android:name="android.intent.action.PACKAGE_REMOVED" />
- <data android:scheme="package" />
- </intent-filter>
- </receiver>
- 其他的一眼明了
- */
- @Override
- public void onReceive(Context context, Intent intent) {
- String action=intent.getAction();
- Uri uri=intent.getData();
- if("android.intent.action.PACKAGE_ADDED".equals(action)){
- Toast.makeText(context, uri.toString()+"被安装了", 0).show();
- }
- if("android.intent.action.PACKAGE_REPLACED".equals(action)){
- Toast.makeText(context, uri.toString()+"被更新了", 0).show();
- }
- if("android.intent.action.PACKAGE_REMOVED".equals(action)){
- Toast.makeText(context, uri.toString()+"被卸载了", 0).show();
- }
- }
- }
- <receiver android:name=".SDStatusReceiver" >
- <intent-filter>
- <action android:name="android.intent.action.MEDIA_MOUNTED" />
- <action android:name="android.intent.action.MEDIA_REMOVED" />
- <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
- <data android:scheme="file"/>
- </intent-filter>
- </receiver>
- 其他一眼明了
- * @author abc
- *
- */
- public class SDStatusReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action=intent.getAction();
- if("android.intent.action.MEDIA_MOUNTED".equals(action)){
- Toast.makeText(context, "SD可用", 0).show();
- }
- if("android.intent.action.MEDIA_REMOVED".equals(action)){
- Toast.makeText(context, "SD被卸载", 0).show();
- }
- if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
- Toast.makeText(context, "SD不可用", 0).show();
- }
- }
- }
- public class SmsReceiver extends BroadcastReceiver {
- /**
- * 思路:
- * 1.拿到短信,封装在intent里面
- * 2.把intent放在Bundle里面
- * 2.以pdus为键,取出短信的内容,保存到Object[]里
- * 3.把object转成byte数组
- * 注:联想系统存储短信的思路:
- * 1.把短信转成字节数组
- * 2.把字节数组存到Object[]数组里面
- *3.以"pdus"为key,把object[]数组储存到Bundle里面,在把Bundle 存储到Intent里面
- *
- *小结:
- *1.AndroidMainfest需要配置的信息receiver android:name=".SmsReceiver">
- <intent-filter android:priority="1000" >
- <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
- </intent-filter>
- </receiver>
- * 2.加权限:<uses-permission android:name="android.permission.RECEIVE_SMS"/>
- */
- @Override
- public void onReceive(Context context, Intent intent) {
- Bundle bundle = intent.getExtras();
- Object[] obj = (Object[]) bundle.get("pdus");
- for (Object object : obj)
- {
- SmsMessage sms=SmsMessage.createFromPdu((byte[]) object);
- if(sms.getOriginatingAddress().equals("1111")){
- abortBroadcast();
- }
- System.out.println(sms.getMessageBody());
- }
- }
- }
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- /**
- * 思路:1.将EditText的字符提取出来
- * 2.创建一个SharedPreferences的存储对象
- * 3.将字符串存储进去(存储的顺序是先获得edit(),然后putstring)
- * 4.编辑,提交
- * @param v
- */
- public void click(View v){
- EditText dt = (EditText) findViewById(R.id.ed);
- SharedPreferences sp=getSharedPreferences("ip", MODE_PRIVATE);
- Editor ed=sp.edit();
- ed.putString("ipNumbe",dt.getText().toString());//键值对的形式,前面是键后面是值
- }
- }
- public class CallReceive extends BroadcastReceiver{
- /**
- * 思路:
- * 1.获得原来的打电话的号码
- * 2.通过上下文的getSharedPreference获得之前保存的数据,即文本框传进来的数据)
- * 3.将看原来的电话加上我们想给他预订的ip
- * 4.将电话重新返回给广播
- */
- @Override
- public void onReceive(Context context, Intent intent) {
- System.out.println("call...");
- String phone=getResultData();
- SharedPreferences sp=context.getSharedPreferences("ip",context.MODE_PRIVATE );
- String ipNumber=sp.getString("ipNumber", "");
- phone=phone+ipNumber;
- setResultData("phone");
- abortBroadcast();
- }
- }
- 同样要在AndroidMainfest配置信息(在系统默认的</activity>之后)
- </span><span style="font-size:12px;"> <receiver android:name=".CallReceive">
- <intent-filter >
- <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
- </intent-filter>
- </receiver>
571

被折叠的 条评论
为什么被折叠?



