当我使用每日重复闹钟时,我的应用程序崩溃了

时间:2017-11-17 12:03:20

标签: java android android-manifest

我试图在修复时间每天发出警报,但是当我点击按钮发出警报时我的应用程序崩溃了。 请帮忙找到解决方案。在此先感谢。

这是我的主要活动代码: -

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

请帮助找到解决方案。任何帮助都将得到赞赏

2 个答案:

答案 0 :(得分:0)

让“Notification_receiver”类像这样公开

    public class Notification_receiver extends BroadcastReceiver               
{
  // your code 
}

它应该有用。

答案 1 :(得分:0)

更新您的清单,如下所示

{{1}}

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

Dint注意到它,同意gautam的回答