android - pendingIntent

发表于:,更新于:,By Sally
大纲
  1. 1. pendingIntent对象
  2. 2. demo
    1. 2.1. 短信应用
    2. 2.2. 系统通知栏

pendingIntent对象

可以通过 getActivity(context, int, intent, int), getBroadcast(context, int, intent, int), getService(context, int, intent, int)来获得pendingIntent对象,它们对应的intent3个行为:跳转到Activity组件,打开一个广播组件,打开一个服务组件。

官网说:the returned object can be handed to other applications so taht they can perform the action you described on your behalf at a later time.

pendingIntent是一种特殊的Intent。不同于Intent的立即执行,pendingIntent可以延迟执行。

by giving a pendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself(with the same permissions and identity).

pendingIntent本身是一个简单的引用,用于检索由系统维护的原始数据的描述。这意味着:尽管意图自己的应用程序进程已经被杀死,从其他进程发出的该处理意图依旧可用。稍后再创建应用并重新获取同样的pendingIntent(same operation, same intent action, data, categories, and components, and same flags),如果之前相同的pendingIntent亦然可用,那么该应用就可以获得pendingIntent的响应,并且可以调用cancel()方法将其移除。

如果你有2个Intent,并且你要为他们每人创建了一个PendingIntent,普遍存在的错误是:你创建了创建多个PendingIntent,而它们只有extra的内容是不同的。而衡量两个pendingIntent是否相同需要看他们是否有 same operation, same intent action, same data, categories, and components, and same flags.

你可以通过Intent.filterEquals来判别两个pendingIntent是否是同一个。或者你可以通过使用不同的请求码来应用这些方法getActivity(context, int, intent, int), getActivities(context, int, intent[], int), getBroadcast(context, int, intent, int), getService(context, int, intent, int).

ps: 还有很多FLAG可以用。FLAG_CANCEL_CURRENT, FLAG_IMMUTABLE, FLAG_NO_CREATE, FLAG_ONE_SHOT, FLAG_UPDATE_CURRENT

demo

短信应用

组件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 两个广播action
private static final String SMS_SEND_ACTION = "com.sally.broadcastdemo.SMS_SEND_ACTION";
private static final String SMS_DELIVER_ACTION = "com.sally.broadcastdemo.SMS_DELIVER_ACTION";

private EditText mPhoneNumber;
private EditText mName;
private Button mSendSms;
private TextView mSendResult;
private TextView mDeliverResult;

private String phoneNumber;
private String name;

private void initEvent() {
mSendSms.setOnClickListener(this);
}

private void initView() {
mPhoneNumber = (EditText) findViewById(R.id.id_number);
mName = (EditText) findViewById(R.id.id_name);
mSendSms = (Button) findViewById(R.id.id_sendSms);
mSendResult = (TextView) findViewById(R.id.send_result);
mDeliverResult = (TextView) findViewById(R.id.deliver_result);
}

发送点击事件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.id_sendSms:
phoneNumber = mPhoneNumber.getText().toString();
name = mName.getText().toString();
if(phoneNumber.isEmpty() || name.isEmpty()) {
Toast.makeText(MainActivity.this, "not null", Toast.LENGTH_SHORT).show();
} else {
sendSms(phoneNumber, name);
}
break;
}
}

发送短信代码:

1
2
3
4
5
6
7
private void sendSms(String phoneNumber, String name) {
SmsManager manager = SmsManager.getDefault();
ArrayList<String> strings = manager.divideMessage(name);
for(String msg : strings) {
manager.sendTextMessage(phoneNumber, null, msg, PendingIntent.getBroadcast(this, 0, new Intent(SMS_SEND_ACTION), 0), PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVER_ACTION), 0));
}
}

在onCreate()方法中注册广播:

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
30
31
32
33
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String msg = "";
switch (getResultCode()) {
case Activity.RESULT_OK:
msg = "message sent successfully.";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
msg = "error.";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
msg = "error: no service";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
msg = "error: not pdu";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
msg = "error: radio off";
break;
}
mSendResult.setText(msg);
}
}, new IntentFilter(SMS_SEND_ACTION));

registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mPhoneNumber.setText("");
mName.setText("");
mDeliverResult.setText("sms delivered");
}
}, new IntentFilter(SMS_DELIVER_ACTION));

系统通知栏

点击事件触发:NotificationCompat.Builder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_favorite_white_24dp)
.setContentTitle("title")
.setContentText("show show show")
.setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;

manager.notify(0, notification);