通知未启动 activity 并在 android 中执行操作
Notification not starting a activity with action in android
我在程序中放了一个按钮。
单击时,会发出通知 。
以下代码运行正常,并发出通知
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button create=(Button)findViewById(R.id.button_creat);
create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
displayNotification(getAppleationContext());
}
});
}
public void displayNotification(Context context)
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("berhooz")
.setContentText("ahmadi hastam")
.setTicker("hello")
.setAutoCancel(true);
Intent notificationIntent = new Intent(context, SecoundActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//single top to avoid //creating many activity stacks queue
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
但我更愿意使用这段代码
Intent notificationIntent = new Intent(context, SecoundActivity.class);
我使用以下代码:
(使用操作调用 activity)
Intent notificationIntent = new Intent("aa.bb.cc.dd");
但是当我点击通知时没有任何反应!
在 AndroidManifest 中:
<activity android:name="SecoundActivity"></activity>
<intent-filter>
<action android:name="aa.bb.cc.dd"></action>
</intent-filter>
要使用自定义操作启动 Activity,您还需要在 intent-filter
中将 category
指定为 DEFAULT
:
<activity android:name="SecoundActivity">
<intent-filter>
<action android:name="aa.bb.cc.dd" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
我在程序中放了一个按钮。 单击时,会发出通知 。 以下代码运行正常,并发出通知
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button create=(Button)findViewById(R.id.button_creat);
create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
displayNotification(getAppleationContext());
}
});
}
public void displayNotification(Context context)
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("berhooz")
.setContentText("ahmadi hastam")
.setTicker("hello")
.setAutoCancel(true);
Intent notificationIntent = new Intent(context, SecoundActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//single top to avoid //creating many activity stacks queue
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
但我更愿意使用这段代码
Intent notificationIntent = new Intent(context, SecoundActivity.class);
我使用以下代码: (使用操作调用 activity)
Intent notificationIntent = new Intent("aa.bb.cc.dd");
但是当我点击通知时没有任何反应!
在 AndroidManifest 中:
<activity android:name="SecoundActivity"></activity>
<intent-filter>
<action android:name="aa.bb.cc.dd"></action>
</intent-filter>
要使用自定义操作启动 Activity,您还需要在 intent-filter
中将 category
指定为 DEFAULT
:
<activity android:name="SecoundActivity">
<intent-filter>
<action android:name="aa.bb.cc.dd" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>