已解决。这个问题由迭戈·
此代码通过一个函数启动通知:通知(最终字符串 mytext(。我们收到带有股票代码,标题,文本,自动取消的通知,...点击它时,运行其他功能:My_Function(我的文本(;
要工作,它不需要修改AndroidManifest,因为使用广播。
public void Notification(final String mytext) {
final Intent notifIntent = new Intent("action_call_method");
PendingIntent pendingmyIntent = PendingIntent.getBroadcast(context, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("action_call_method")) {
My_Function(mytext);
}
}
};
IntentFilter filter = new IntentFilter("action_call_method");
context.registerReceiver(receiver, filter);
Notification noti = new Notification.Builder(context)
.setTicker(ticker)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_lock_silent_mode_off)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setAutoCancel(true)
.setContentIntent(pendingmyIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
为PendingIntent
中的Intent
设置自定义操作并向其注册接收器,然后您就可以执行所需的操作:
Intent notifIntent = new Intent("action_call_method");
PendingIntent pendingmyIntent = PendingIntent.getActivity(context, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);
然后注册一个BroadcastReceiver
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("action_call_method")) {
// Call your method here
}
}
};
IntentFilter filter = new IntentFilter("action_call_method");
registerReceiver(receiver, filter);
不要忘记取消注册您的接收器。