2015-06-06 103 views
0

我知道,我問的問題不斷重複,但我已經嘗試了一切,閱讀每個Q/A上。點擊通知後仍無法開始新活動。我正在使用NotificationCompat。無法啓動活動後點擊android通知

下面是我的主要活動

package notification.test.example.com.notificationtest; 

import android.app.Activity; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.BitmapFactory; 
import android.provider.Settings; 
import android.support.v4.app.NotificationCompat; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 


public class MainActivity extends Activity { 

    private static final String NOTIFICATION_CONTENT_TITLE = "Notification Test"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button button = (Button)findViewById(R.id.btn1); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       loadNotification(); 
      } 
     }); 
    } 

    private void loadNotification() { 

     int requestId = (int)System.currentTimeMillis(); 
     Intent intent = new Intent(getApplicationContext(), NotificationDetailsActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.putExtra("msg", "Notification Details activity launched"); 

     PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), requestId, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 

     NotificationManager notificationManager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()). 
       setSmallIcon(R.drawable. abc_btn_check_material).setContentTitle(NOTIFICATION_CONTENT_TITLE) 
       .setSound(Settings.System.DEFAULT_NOTIFICATION_URI) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText("Welcome")) 
       .setAutoCancel(true) 
       .setContentText("This is Notification test") 
       .setContentIntent(pendingIntent) 
       .setSubText("-------------------This is notification sub text area-------------------") 
       .setTicker("Notify Title"); 

     notificationManager.notify(1, builder.build()); 
    } 
} 

下面是我的DetailNotification的java文件。

package notification.test.example.com.notificationtest; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.PersistableBundle; 
import android.util.Log; 
import android.widget.Toast; 

/** 
* Created by sony on 06-06-2015. 
*/ 
public class NotificationDetailsActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 
     super.onCreate(savedInstanceState, persistentState); 

     setContentView(R.layout.notification_layout); 
     Intent intent = getIntent(); 
     String data = intent.getStringExtra("msg"); 
     Log.d(NotificationDetailsActivity.class.getName(),data+"--------------------------------------------------------------"); 
     Toast.makeText(getApplicationContext(), "Message : "+data, Toast.LENGTH_LONG).show(); 
    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="notification.test.example.com.notificationtest" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".NotificationDetailsActivity" 
      android:label="Notification Detail Activity"></activity> 
    </application> 

</manifest> 

的build.gradle文件。

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 21 
    buildToolsVersion "21.1.2" 

    defaultConfig { 
     applicationId "notification.test.example.com.notificationtest" 
     minSdkVersion 15 
     targetSdkVersion 21 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:22.0.0' 
} 

專家請指導我,我錯了,我已經花了5-6小時。

回答

2

可能嘗試這種istead下面一行:

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), requestId, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 

使用此行:

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

編輯

而且更重要的是你能做的僅僅是刪除第二參數從NotificationDetailActivity類和super.onCreate()調用方法也

+0

這些上面兩行有什麼不同?我也試過你的建議,仍然存在問題。 – ved

+0

你的通知活動需要調用api 21.你正在開發棒棒糖? – Pankaj

+0

我的targetsdk版本是21,我也在棒棒糖上測試它。這是問題嗎? – ved

0

嘗試增加其他活動......也許我們的代碼應該這樣

+0

另一種活動的意思是? – ved

+0

第二項活動,將其設置在清單上......這個問題也發生在我身上.. – Jenron19

+0

它已經在清單文件中,現在我已經更新了我的問題。 – ved

0

我用Intent.FLAG_ACTIVITY_NEW_TASKexample,對我的工作完美工作。

Intent myIntent = new Intent(context, DoSomething.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(
     context, 
     0, 
     myIntent, 
     Intent.FLAG_ACTIVITY_NEW_TASK);