单击android通知后无法启动活动

时间:2015-06-06 12:38:33

标签: java android android-activity android-studio notifications

我知道,我一直在问问题,但我已经尝试了所有内容,并在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小时。

3 个答案:

答案 0 :(得分:2)

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

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()调用methos

答案 1 :(得分:0)

尝试添加其他活动...也许Ur代码应该以这种方式工作

答案 2 :(得分:0)

我在我的example上使用了 Intent.FLAG_ACTIVITY_NEW_TASK ,并且完美地为我工作。

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