广播接收者(大学作业二)
实验项目: |
广播接收者 |
||
实验地点: |
|
实验时间: |
|
一、实验目的: 1.掌握Android中四大组件中的广播接收者的概念和用法 2.熟练掌握Android中广播接收者中有关系统广播的处理。 二、实验内容与要求 1.功能需求 2.1 实现SD卡监听的过程并给出输出结果 2.2 实现短信监听的过程并给出输出结果 2.3 简单实现打电话广播事件的监听。 注意:1.输出结果为System.out.printIn()输出即可 2.写出详细的实现过程,要求源代码和文字说明 三、实验步骤和结果: MainActivity.java: package com.lgqchinese.homework2; public class MainActivity extends AppCompatActivity { EditText phoneNumber; Button callPhone; private IntentFilter intentFilter; private SmsReceiver smsBroadcastReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view) { String ipnumber = phoneNumber.getText().toString().trim(); SharedPreferences sp = getSharedPreferences("config", 0); sp.edit().putString("ipnumber", ipnumber).commit(); Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show(); } } SdCardStartReceiver.java: public class SdCardStartReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if ("android.intent.action.MEDIA_MOUNTED".equals(action)){ System.out.println("SD卡被挂载"); }else if ("android.intent.action.MEDIA_UNMOUNTED".equals(action)){ System.out.println("SD卡卸载了"); } } } SmsReceiver.java: public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Object[] objects = (Object[]) bundle.get("pdus"); for (Object pdus : objects) { SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus); String address = sms.getOriginatingAddress(); String body = sms.getMessageBody(); System.out.println(address+";dddd"+body); } } } CallPhoneReceiver.java: public class CallPhoneReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { SharedPreferences sp = context.getSharedPreferences("config",0); String ipNumber = sp.getString("ipnumber",""); String currentNumber = getResultData(); // if (currentNumber.startsWith("0")){ setResultData(ipNumber+currentNumber); System.out.println(ipNumber+currentNumber); // } } } 广播注册及添加权限: <receiver android:name=".SdCardStartReceiver"> <intent-filter> <action android:name="android.intent.action.MEDIA_MOUNTED"/> <action android:name="android.intent.action.MEDIA_UNMOUNTED"/> <data android:scheme="file"/> </intent-filter> </receiver> <receiver android:name=".SmsReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> <receiver android:name=".CallPhoneReceiver"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> Sd卡 短信: 电话加前缀 |
|||
四、实验总结: 通过本次实验,熟悉了广播接受者的原理和使用方法。Android6.0以后需要动态获取权限,需要再进一步学习。 |