我知道,我问的是重复的问题,但我已经尝试了所有方法并阅读了有关SO的每个Q / A。单击通知时仍无法启动新活动。我正在使用NotificationCompat。

以下是我的主要活动

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个小时。

最佳答案

可以试试这个,而不是下面的行:

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()中删除第二个参数。

10-07 15:50