我在程序中放了一个按钮。 单击时,会发出通知。 以下代码正常运行,并发出通知。
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);
我使用以下代码: (使用操作调用活动)
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>
答案 0 :(得分:0)
要使用自定义操作启动活动,您还需要在category
中将DEFAULT
指定为intent-filter
:
<activity android:name="SecoundActivity">
<intent-filter>
<action android:name="aa.bb.cc.dd" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>