Android中的通知 - 应用程序在notify()后停止

时间:2017-03-11 11:30:30

标签: java android xml android-studio notifications

我有一个简单的Android应用程序在屏幕上显示通知。应用程序正常运行,直到builder.setContentIntent()并正确显示Toast消息。如果没有注释管道,manager.notify(),则App停止。我正在使用Android Studio。

MainActivity.java

这是我的通知代码

private void addNotification() {
        message = txtMessage.getText().toString();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("You have a new message").setContentText(message);
        // Add as notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(this, MainActivity.class);

       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        Toast.makeText(getApplicationContext(), "contentIntentset",
                Toast.LENGTH_SHORT).show();

        //manager.notify(0, builder.build());

}

的AndroidManifest.xml

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NotificationView"
            android:label="Details of notification"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
    </application>

有人可以帮忙吗?

0 个答案:

没有答案