簡體   English   中英

通知中未決意圖的問題

[英]Problems with a pending intent in a notification

我想在單擊通知時打開一個活動(Repeating_activity),但是當我單擊通知時應用程序崩潰了。

嘗試使用各種意圖標志組合更改通知 ID

MainActivity.java:

public class MainActivity extends AppCompatActivity {

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

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 6);
        calendar.set(Calendar.MINUTE, 33);
        calendar.set(Calendar.SECOND, 0);

        Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 12, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HALF_DAY, pendingIntent);

    }
}

警報接收器.java:

public class AlarmReceiver extends BroadcastReceiver {
    private final String CHANNEL_ID = "notifications_monitor";
    private final String CHANNEL_NAME = "NotificationMonitorExperiment";
    private final String CHANNEL_DESC = "An experiment to gather mood changes from notifications";

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent repeating_intent = new Intent(context, Repeating_activity.class);

        repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 12, repeating_intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_notifications_none_24px)
                .setContentTitle("Notifications Monitor Experiment")
                .setContentText("Please answer a few questions about yourself")
                .setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
            channel.setDescription(CHANNEL_DESC);

            notificationManager.createNotificationChannel(channel);
        }

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

repeating_activity.java

class Repeating_activity extends AppCompatActivity {
    @Override

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repeating_activity_layout);
    }
}

日志貓:

 2019-10-06 19:23:17.946 17895-17895/com.example.notifications W/ActivityThread: handleWindowVisibility: no activity for token

android.os.BinderProxy@d33941 2019-10-06 19:23:17.950 17895-17895/com.example.notifications D/AndroidRuntime: 關閉 VM 2019-10-06 19:23:17.952 17895-com.example.notifications .notifications E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.notifications, PID: 17895 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.notifications/com.example.notifications.Repeating_activity}: java. lang.IllegalAccessException: java.lang.Class is not accessible from java.lang.Class at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3046) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.Z93F725A07423FE1C889F448B 33D21F46Z:3258) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor .java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java: 193) 在 android.app.ActivityThread.main(ActivityThread.java:7029) 在 java.lang.reflect.Method.Method. hod) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.IllegalAccessException : java.lang.Class is not accessible from java.lang.Class at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) at androidx.core.app.CoreComponentFactory .instantiateActivity(CoreComponentFactory.java:41) 在 android.app.Instrumentat ion.newActivity(Instrumentation.java:1232) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3034) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3258) at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.句柄消息(ActivityThread.java:1955)在 android.os.Handler.dispatchMessage(Handler.Z9 3F725A07423FE1C889F448B33D21F46Z:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:7029) at java.lang.reflect.Method.invoke(Native Method) at com .android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 2019-10-06 19:23:17.956 17895- 17895/com.example.notifications W/OPDiagnose:getService:OPDiagnoseService NULL 2019-10-06 19:23:17.960 17895-17974/com.example.notifications D/OSTracker:操作系統事件:崩潰 2019-10-2 :17.981 17895-17895/com.example.notifications I/Process:發送信號。 PID:17895 SIG:9

您可以嘗試添加到 AndroidManifest.xml 文件嗎

<application
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme">
    .... 
    <activity
        android:name=".Repeating_activity"
        android:label="@string/app_name"/>

</application

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM