我已经搜索了很多东西,即使遵循了许多教程,似乎也找不到解决方案。
Intent testIntent = new Intent(this, ActionFromNotification.class);
PendingIntent pendIntent = PendingIntent.getBroadcast(this, 0, testIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.pulp_orange_icon)
.setContentTitle("Pulp")
.setContentText("Hi")
.setStyle(new NotificationCompat.BigTextStyle().bigText("hello"))
.addAction(R.drawable.ic_launcher, "testbutton1",pendIntent)
.setTicker("TICKER").setContentTitle("TITLE").setContentText("CONTENT")
.setWhen(System.currentTimeMillis()).setAutoCancel(false)
.setOngoing(true).setPriority(Notification.PRIORITY_HIGH);
// .setContentIntent(pendIntent);
Notification mNotification = builder.build();
startForeground(1337, mNotification);
public class ActionFromNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
Log.d("BYI", "end button pressed from notification");
}
}
//Manifest File
<receiver android:name=".ActionFromNotification"/>
当我单击通知中的“ testbutton1”(显示正确)时,我应该只得到一个log.d响应...但是什么也看不到。
我认为您需要做的是通过操作而不是诸如以下这样的类来创建初始意图:
Intent testIntent = new Intent("com.example.my_project.test_intent");
然后根据需要设置PendingIntent:
PendingIntent pendIntent = PendingIntent.getBroadcast(this, 0, testIntent, PendingIntent.FLAG_UPDATE_CURRENT);
在清单中设置一个意图过滤器来触发接收者
<receiver android:name=".ActionFromNotification"> // note the removal of the / in />
<intent-filter>
<action android:name="com.example.my_project.test_intent" />
</intent-filter>
</receiver>
重新读取了接收者标签的文档后,似乎它也应该与类名一起使用,在这种情况下,您应该尝试在原始接收者标签上使用该类的全名,即“ fullly_qualified_package_name.ActionFromNotification”
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句