Android My BroadcastReceiver类否接收操作发送自通知
在我的IntentService类中,我创建了一个通知并分配ID=1213,一旦应用程序打开,通知就会显示出来Android My BroadcastReceiver类否接收操作发送自通知,android,broadcastreceiver,intentservice,Android,Broadcastreceiver,Intentservice,在我的IntentService类中,我创建了一个通知并分配ID=1213,一旦应用程序打开,通知就会显示出来 Intent cancelScan = new Intent(); cancelScan.setAction(CANCEL); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1213, cancelScan, PendingIntent.FLAG_UPDATE_CURRENT);
Intent cancelScan = new Intent();
cancelScan.setAction(CANCEL);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1213, cancelScan, PendingIntent.FLAG_UPDATE_CURRENT);
mNbuilder.addAction(android.R.drawable.ic_menu_close_clear_cancel,"Cancel Scanning",pendingIntent);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, mNbuilder.build());
在我的课堂上
if(CANCEL.equals(intent.getAction())){
Log.i(TAG,"Received Broadcasr Receiver");
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//context.stopService(new Intent(context,ScanService.class));
nm.cancel(NOTIFICATION_ID);
}
}
还有我的Manifest.XML
<receiver android:name=".WifiScanReceiver" android:enabled="true">
<intent-filter>
<action android:name="CANCEL"/>
</intent-filter>
</receiver>
当我点击通知下方的操作按钮时,我已经尝试了几次,但是Logcat没有打印任何内容。哪些部分我做错了?提前感谢。清单中的
CANCEL
值和声明为action(“CANCEL”)的值必须相等,否则无效。你的不,这就是为什么你没有达到你的if声明。您的接收器会被触发,因为您发送的广播动作正确
为了确保在代码中使用正确的值,您可以在wifiscanever
中声明静态final:
public class WifiScanReceiver {
public static final String CANCEL = "CANCEL";
...
}
因此,您也可以在发送广播时在代码中使用它:
Intent cancelScan = new Intent();
cancelScan.setAction(WifiScanReceiver.CANCEL);
这样,您就可以始终确定使用相同的值。您必须确保的唯一一个也是正确的,就是您的清单中的一个。此行中
CANCEL
的值是什么:cancelScan.setAction(CANCEL)
private final String CANCEL=“CANCEL”,变量和值均使用相同的名称。感谢您的回复:)您是否也确定您的BroadcastReceiver
中的CANCEL
的值相同?是的,我声明为私有静态最终字符串Yes\u ACTION=“com.example.packagename.CANCEL”;我把“packagename”也重命名为我的项目名。我不太明白。我想知道这一行的CANCEL
的值:if(CANCEL.equals(intent.getAction()){
。如果这确实是“com.example.packagename.CANCEL”,那么这将不起作用,因为您的操作不匹配。谢谢,因为我在这里被引用了