标签:
1.默认广播
MainActivity
package com.example.broadcastreceivertest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendNormalClick(View v) { Intent intent = new Intent( "com.example.broadcastreceivertest.MY_ACTION"); intent.putExtra("info", "我是一条普通广播"); this.sendBroadcast(intent); } }
MyBroadcastReceiver
package com.example.broadcastreceivertest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastReceiver { // public MyBroadcastReceiver(){}//构造方法,可以不写 @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String info = intent.getStringExtra("info"); Toast.makeText(context, info, Toast.LENGTH_SHORT).show(); } }
AndroidMainfest.xml
<receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcastreceivertest.MY_ACTION"/> </intent-filter> </receiver>
在onResume()注册广播,在onPause()解除注册广播,注册方法:在onResume外创建一个接收器的实例receiver,然后在onResume创建一个IntentFilter的实例filter,调用filter的addAction方法,再调用registerReceiver方法,传入receiver和filter
解除注册调用unregisterReceiver方法,传入receiver参数
3.有序广播
MainActivity
package com.example.orderbroadcastreceivertest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendOrderClick(View v){ Intent intent = new Intent("com.example.orderbroadcastreceivertest.MY_BROADCAST"); this.sendOrderedBroadcast(intent, null); } }
MyReceiver1
package com.example.orderbroadcastreceivertest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class MyReceiver1 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Bundle data = getResultExtras(false); String info = data.getString("info"); Toast.makeText(context, "有序广播--1"+info, Toast.LENGTH_SHORT).show(); } }
MyReceiver2
package com.example.orderbroadcastreceivertest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class MyReceiver2 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "有序广播--2", Toast.LENGTH_SHORT).show(); Bundle data = new Bundle(); data.putString("info", "广播传值"); this.setResultExtras(data); this.abortBroadcast(); } }
AndroidMainfest.xml
<receiver android:name=".MyReceiver1"> <intent-filter android:priority="100"> <action android:name="com.example.orderbroadcastreceivertest.MY_BROADCAST"></action> </intent-filter> </receiver> <receiver android:name=".MyReceiver2"> <intent-filter android:priority="200"> <action android:name="com.example.orderbroadcastreceivertest.MY_BROADCAST"></action> </intent-filter> </receiver>
4.粘性广播
5.接收开机启动广播和网络状态变化广播
注:<action android:name="android.net.conn.CONNECTIVITY_CHAGE"/>不是系统提供的,要手动添加
6.接收电量变化广播
7.发送短信
MainActivity
package com.example.smstest; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendClick(View v) { // 获取短信管理器 android.telephony.SmsManager manager = android.telephony.SmsManager .getDefault(); String sms = "我是一条短信"; // 拆分长短信 ArrayList<String> text = manager.divideMessage(sms); for (int i = 0; i < text.size(); i++) { manager.sendTextMessage("131****2160", null, text.get(i), null, null); } } }
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
8.接收短信
9.AlarmManager服务
MainActivity
package com.example.alarmtest; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void startClick(View v) { AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); long triggerTime = System.currentTimeMillis() + 3000; Intent intent = new Intent(this, MyReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); am.set(AlarmManager.RTC, triggerTime, pi); // am.setRepeating(AlarmManager.RTC, triggerTime, 8000, pi); } }
package com.example.alarmtest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "起床啦!", Toast.LENGTH_LONG).show(); } }
<receiver android:name=".MyReceiver"></receiver>
标签:
原文地址:http://blog.csdn.net/huangluqian/article/details/50990273