通知
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| MainActivity.java
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder=new Notification.Builder(this);
Intent intent=new Intent(this,NotificationActivity.class);
PendingIntent pi=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle("Notification Title"); builder.setContentText("Notification text"); builder.setContentIntent(pi);
notificationManager.notify(11, builder.build());
|
- FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
- FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
- FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
- FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。
1 2 3 4 5 6
| NotificationActivity.java
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(11);
|
1 2 3 4 5 6 7 8
| Uri soundUri=Uri.fromFile(new File("/system/media/audio/ringtones/Boxbeat.ogg")); builder.setSound(soundUri);
long [] vibrates={0,1000,1000,1000,1000,1000}; builder.setVibrate(vibrates);
|
效果

短信
接收短信
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| mReceiveFilter=new IntentFilter(); mReceiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); mMessageReceiver=new MessageReceiver(); registerReceiver(mMessageReceiver,mReceiveFilter);
class MessageReceiver extends BroadcastReceiver{
@Override public void onReceive(Context context, Intent intent) { Bundle bundle=intent.getExtras();
Object[] pdus=(Object[])bundle.get("pdus");
SmsMessage[] messages=new SmsMessage[pdus.length];
for(int i=0;i<messages.length;i++){ messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]); }
String address=messages[0].getOriginatingAddress();
String fullMessage=""; for(SmsMessage message:messages){ fullMessage+=message.getMessageBody(); } sender.setText(address); content.setText(fullMessage); } }
|
拦截短信
系统发出的短信广播是一条有序广播,所以可以先获得该广播,再中止广播传递即可。
1.提高MessageReceiver的优先级,让它能够先于系统短信程序接收到短信广播。
2.调用abortBroadcast()方法,中止广播传递。
发送短信
1 2 3 4 5 6 7 8 9
| mSendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("AAABBB", "before"); SmsManager smsManager = SmsManager.getDefault(); Log.i("AAABBB", "getDefault"); smsManager.sendTextMessage(mTo.getText().toString(), null, mEditText.toString(), null, null); } });
|