当我使用 Daily repeating Alarm 时,我的应用程序崩溃了

My Application Is crashing when I'm using Daily repeating Alarm

我试图每天在固定时间发出警报,但当单击按钮发出警报时,我的应用程序崩溃了。 请帮助找到解决方案。提前致谢。

这是我的主要 Activity 代码:-

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import java.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Calendar calendar = Calendar.getInstance();

                calendar.set(Calendar.HOUR_OF_DAY,15);
                calendar.set(Calendar.MINUTE,9);
                calendar.set(Calendar.SECOND,20);


                Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);

                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

            }
        });
    }
}

这是我的 Notification_receiver.java 文件:-

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;



class Notification_receiver extends BroadcastReceiver {
    @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,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setContentTitle("Notification Tittle")
                .setContentText("Notification Text")
                .setAutoCancel(true);

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

如果需要,这里是清单 :-

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hacker.timernotification">

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> //I tried with and without this but still crashing my app
    <uses-permission android:name="android.permission.WAKE_LOCK"/> // this is at the time of fire alarm

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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=".Repeating_activity"/>
        <receiver android:name=".Notification_receiver"></receiver>
            <!--android:enabled="true"
            android:exported="true-->"/>
    </application>

</manifest>

这是 Repeating_activity.java 文件 :-

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;



class Repeating_activity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repeating_activity_layout);
    }
}

activity_main.xml 和 repeating_activity_layout.xml 我的项目中没有粘贴在这里,因为我认为没有必要。

这是我的应用程序崩溃后出现的错误:-

16297-16297/com.example.hacker.timernotification E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      java.lang.RuntimeException: Unable to instantiate receiver com.example.hacker.timernotification.Notification_receiver: java.lang.IllegalAccessException: access to class not allowed
                                                                                          at android.app.ActivityThread.handleReceiver(ActivityThread.java:2436)
                                                                                          at android.app.ActivityThread.access00(ActivityThread.java:157)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1365)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                          at android.os.Looper.loop(Looper.java:176)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5317)
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                                       Caused by: java.lang.IllegalAccessException: access to class not allowed
                                                                                          at java.lang.Class.newInstanceImpl(Native Method)
                                                                                          at java.lang.Class.newInstance(Class.java:1319)
                                                                                          at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
                                                                                          at android.app.ActivityThread.access00(ActivityThread.java:157) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1365) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                          at android.os.Looper.loop(Looper.java:176) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5317) 
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                          at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                                                          at dalvik.system.NativeStart.main(Native Method) 

请帮忙寻找解决办法。任何帮助将不胜感激

让你的"Notification_receiver"classpublic像这样

    public class Notification_receiver extends BroadcastReceiver               
{
  // your code 
}

应该可以。

如下更新您的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hacker.timernotification">

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    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=".Repeating_activity"/>
    <receiver android:name=".Notification_receiver"
         android:directBootAware="true">
          <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

</application>

bootAware 是必需的,因为您的闹钟在重新启动设备后将被禁用,为了再次启用它必须使用。希望这有帮助

没注意到,同意gautam的回答