2015-02-07 127 views
0

我在程序中放了一個按鈕。 單擊時,會發出通知。 以下代碼正常運行,併發出通知。通知沒有開始一個活動在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); 

我使用下面的代碼: (使用動作呼叫活動)

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

要使用自定義操作開始的活動,你還需要在intent-filter指定categoryDEFAULT

<activity android:name="SecoundActivity"> 
     <intent-filter> 
      <action android:name="aa.bb.cc.dd" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
</activity> 
+0

謝謝您的幫助 – mariya 2015-02-07 15:37:51