简体   繁体   中英

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

I am trying to fire alarm everyday on fix time but my application is crashing when click the button to fire alarm. Please help to find solution. Thanks in Advance .

Here is my Main Activity code :-

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);

            }
        });
    }
}

Here is my Notification_receiver.java file:-

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());
    }
}

Here is Manifest if needed :-

<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>

Here is Repeating_activity.java file :-

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 and repeating_activity_layout.xml I have in my projecte not pasting here because I don't think it's need.

Here is error getting after crashing my application :-

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.access$1600(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.access$1600(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) 

Please help to find solution . Any help will be Appreciated

Make your "Notification_receiver" class public like this

    public class Notification_receiver extends BroadcastReceiver               
{
  // your code 
}

it should work.

Update your manifest as below

<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 is required because your alarm will get disabled after restarting your device, in order to enable it again will have to use. Hope this helps

Dint notice it, agreed with gautam's answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM