2015-06-06 150 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);