我有一个视图,我想在点击通知时使其可见和不可见。我已成功创建通知但无法在其上创建点击事件。这是我的代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(android.R.drawable.btn_plus);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
mBuilder.setOngoing(true);
Intent resultIntent = new Intent(this, BroadcastTV.class);
resultIntent.setAction("set_tv");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = PendingIntent.getBroadcast(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationID, mBuilder.build());
BroadcastTV:
public class BroadcastTV extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action_name = intent.getAction();
Toast.makeText(context, "working fine ...", Toast.LENGTH_SHORT).show();
if (action_name.equals("set_tv")) {
if (MainActivity.isVisible)
{
context.stopService(new Intent(context, FlyBitch.class));
MainActivity.isVisible = false;
}
else if (!MainActivity.isVisible)
{
context.startService(new Intent(context, FlyBitch.class));
MainActivity.isVisible = true;
}
}
}
}
答案 0 :(得分:0)
在AndroidManifest文件中声明BroadcastReceiver,如下面的示例所示
<receiver android:name="com.app.packagename.BroadcastTV"></receiver>