重启时启动警报管理器应用程序

时间:2014-07-22 16:07:31

标签: android broadcastreceiver alarmmanager android-alarms autostart

目前,我正在开发应用程序,可在选定的时间将手机设置为振动模式或振铃模式。我已经在我的应用程序中使用alarmmanager成功实现了这一点。 我希望我的应用程序在重新启动手机时记住所有待处理的意图。 我在互联网上找到了一个示例代码,但是当手机重启时,它似乎会崩溃我的应用程序。我不知道出了什么问题。

这是alarmreciever.java

package ishan.khandelwal.vitsilentmode;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.widget.Toast;

public class AlarmReciever extends BroadcastReceiver
{
private AudioManager mAudioManager;

@Override
        public void onReceive(Context context, Intent intent)
        {
                // TODO Auto-generated method stub

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceIntent = new Intent("ishan.khandelwal.vitsilentmode");
        context.startService(serviceIntent);
        }   

        mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

        // Show the toast
        Toast.makeText(context, "Vibration Mode", Toast.LENGTH_SHORT).show();

          }
}

Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ishan.khandelwal.vitsilentmode"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

  <!-- permission required to use Alarm Manager -->
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />




<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver
    android:name=".receiver.StartMyServiceAtBootReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="StartMyServiceAtBootReceiver">
     <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>


    <activity
        android:name="ishan.khandelwal.vitsilentmode.MainActivity"
        android:label="@string/app_name" 
       >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="ishan.khandelwal.vitsilentmode.GetSlots"
        android:label="Select your slots"
        android:screenOrientation="portrait" >


    </activity>

     <activity
        android:name="ishan.khandelwal.vitsilentmode.MorningSlots"
        android:label="Select your morning slots"
        android:screenOrientation="portrait" >


    </activity>

      <activity
        android:name="ishan.khandelwal.vitsilentmode.EveningSlots"
        android:label="Select your evening slots"
        android:screenOrientation="portrait" >


    </activity>

       <activity
        android:name="ishan.khandelwal.vitsilentmode.Labs"
        android:label="Select your lab slots"
        android:screenOrientation="portrait" >


    </activity>

       <activity
        android:name="ishan.khandelwal.vitsilentmode.About"
        android:label="About"
        android:screenOrientation="portrait" >


    </activity>

     <activity
        android:name="ishan.khandelwal.vitsilentmode.AlarmReciever"
        android:label="AlarmReciever" >


    </activity>

       <!-- Register the Alarm Receiver -->
               <receiver android:name=".AlarmReciever"/>

      <activity
        android:name="ishan.khandelwal.vitsilentmode.RingerMode"
        android:label="RingerMode" >


    </activity>

       <!-- Register the Alarm Receiver -->
               <receiver android:name=".RingerMode"/>
</application>

logcat的:

07-23 23:33:12.057: I/ActivityManager(858): Start proc com.android.providers.calendar for broadcast com.android.providers.calendar/.CalendarReceiver: pid=1116 uid=10023 gids={3003}
07-23 23:33:12.067: I/ActivityThread(1116): Pub com.android.calendar: com.android.providers.calendar.CalendarProvider2
07-23 23:33:12.087: I/SurfaceFlinger(858): Boot is finished (2041 ms)
07-23 23:33:12.128: I/ActivityManager(858): Start proc ishan.khandelwal.vitsilentmode for broadcast ishan.khandelwal.vitsilentmode/.receiver.StartMyServiceAtBootReceiver: pid=1127 uid=10031 gids={}
07-23 23:33:12.137: D/AndroidRuntime(1127): Shutting down VM
07-23 23:33:12.137: W/dalvikvm(1127): threadid=1: thread exiting with uncaught exception (group=0xb6fac4f0)
07-23 23:33:12.137: E/AndroidRuntime(1127): FATAL EXCEPTION: main
07-23 23:33:12.137: E/AndroidRuntime(1127): java.lang.RuntimeException: Unable to instantiate receiver ishan.khandelwal.vitsilentmode.receiver.StartMyServiceAtBootReceiver: java.lang.ClassNotFoundException: ishan.khandelwal.vitsilentmode.receiver.StartMyServiceAtBootReceiver in loader dalvik.system.PathClassLoader[/data/app/ishan.khandelwal.vitsilentmode-1.apk]
07-23 23:33:12.137: E/AndroidRuntime(1127):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1773)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at android.os.Looper.loop(Looper.java:130)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at java.lang.reflect.Method.invoke(Method.java:507)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at dalvik.system.NativeStart.main(Native Method)
07-23 23:33:12.137: E/AndroidRuntime(1127): Caused by: java.lang.ClassNotFoundException: ishan.khandelwal.vitsilentmode.receiver.StartMyServiceAtBootReceiver in loader dalvik.system.PathClassLoader[/data/app/ishan.khandelwal.vitsilentmode-1.apk]
07-23 23:33:12.137: E/AndroidRuntime(1127):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
07-23 23:33:12.137: E/AndroidRuntime(1127):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1764)
07-23 23:33:12.137: E/AndroidRuntime(1127):     ... 10 more

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好的,问题是您在AndroidManifest中指定了错误/不存在的接收器实现。你指定了这个:

<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
 <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>

问题是你没有名为StartMyServiceAtBootReceiver的类。你认为你应该拥有的是这样的:

<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
 <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>