1.广播接收器:
public class MyReceiver1 extends BroadcastReceiver {
public MyReceiver1() {
}
@Override
public void onReceive(Context context, Intent intent) {
String i = intent.getStringExtra("info");
Toast.makeText(context, i, Toast.LENGTH_SHORT).show();
}
}
2.activity类:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendNormal(View v) {
Intent intent = new Intent("com.ke.action.MY_BROADCAST");
intent.putExtra("info", "this is boradcast");
this.sendBroadcast(intent);
}
}
public class MyReceiver2 extends BroadcastReceiver {
public MyReceiver2() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "动态注册的广播接收器", Toast.LENGTH_SHORT).show();
}
}
public class MainActivity extends Activity {
private MyReceiver2 myReceiver2 = new MyReceiver2();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("com.ke.action.MY_BROADCAST");
registerReceiver(myReceiver2, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(myReceiver2);
}
}
1.activity类
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendOrderClick(View v) {
Intent intent = new Intent("com.andorid.keke");
this.sendOrderedBroadcast(intent, null);
}
}
2.广播接收器
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "有序广播接收器-1",Toast.LENGTH_SHORT).show();
Bundle data = new Bundle();
data.putString("info","广播1");
this.setResultExtras(data);
}
}
public class MyReceiver2 extends BroadcastReceiver {
public MyReceiver2() {
}
@Override
public void onReceive(Context context, Intent intent) {
Bundle data = getResultExtras(false);
Toast.makeText(context, data.getString("info").toString(), Toast.LENGTH_SHORT).show();
}
}
3.配置清单文件:
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="1000">
<action android:name="com.andorid.keke"></action>
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver2"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="200">
<action android:name="com.andorid.keke"></action>
</intent-filter>
</receiver>
public void mmsClick(View v){
SmsManager sm = SmsManager.getDefault();
String message = "一条短线";
ArrayList<String> list = sm.divideMessage(message);
for (int i=0;i<list.size();i++){
sm.sendTextMessage("5556",null,list.get(i),null,null);
}
}
public class MyReceiverR extends BroadcastReceiver {
public MyReceiverR() {
}
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle !=null){
Object[] objects = (Object[]) bundle.get("pdus");
SmsMessage[] smsMessages = new SmsMessage[objects.length];
for (int i=0;i<objects.length;i++){
smsMessages[i] = SmsMessage.createFromPdu((byte[]) objects[i]);
String number = smsMessages[i].getDisplayOriginatingAddress();
String cont = smsMessages[i].getDisplayMessageBody();
Toast.makeText(context,number +":"+cont,Toast.LENGTH_SHORT).show();
}
}
}
}
<receiver
android:name=".MyReceiverR"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
activity:
public void startAlarmClick(View v){
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long triggerTime = System.currentTimeMillis()+3000;
Intent intent = new Intent(this,MyReceiverR.class);
PendingIntent op = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC,triggerTime,2000,op);
}
2.广播接收器:
public class MyReceiverR extends BroadcastReceiver {
public MyReceiverR() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"闹钟",Toast.LENGTH_SHORT).show();
}
}
1.activity:
public void startSet(View v){
AlarmManager ams = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long triggerTime = System.currentTimeMillis() + 3000;
Intent intent = new Intent(this,MainActivity2Activity2.class);
startActivity(intent);
}
2.另一个activity,打开音乐播放器,并自动调用对话框:
public class MainActivity2Activity2 extends Activity {
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_main_activity2_activity2);
mp = new MediaPlayer();
try {
mp.setDataSource(this, Uri.parse("/sdcard/music/nl.mp3"));
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.setLooping(true);
mp.start();
alarmDialog();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mp !=null){
if(mp.isPlaying()){
mp.stop();
}
mp.release();
}
}
public void alarmDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("起床了");
builder.setPositiveButton("再来一次", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
alarm();
finish();
}
});
builder.setNegativeButton("关闭了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
private void alarm(){
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long triggerTime = System.currentTimeMillis()+5000;
Intent intent = new Intent(this,MainActivity2Activity2.class);
PendingIntent op = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC,triggerTime,op);
}
}