使用警报管理器 - 强制关闭错误[android]

时间:2014-05-17 21:12:28

标签: java android alarmmanager logcat

尝试使用闹钟管理器来安排我的代码'每天运行,我按照教程,并尝试适应我的代码。

两点:

1)我是否在逻辑上以正确的方式使用报警管理器,在哪里放置受报警管理器影响的代码以及不正常运行的代码,以及那里的一些信息。

2)尝试并修复日志cat错误,因为您可能已经意识到我是Android开发的新手,所以我正在努力修复/找到错误。 - 任何有关logcat查找错误的见解也很有用。

这是我的' MainACtivity.java':

public class MainActivity extends Activity {

    private PendingIntent pendingIntent;

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

                Intent myIntent = new Intent(MainActivity.this, Alarm.class);
                pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);



        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);



        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.add(Calendar.SECOND, 10);

        alarmManager.set(Calendar.HOUR_OF_DAY, 0, pendingIntent);

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);         // time set up is unsuccessful in earlier versions

            }       @Override   public boolean onCreateOptionsMenu(android.view.Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        super.onCreateOptionsMenu(menu);        getMenuInflater().inflate(R.menu.main, menu);       return true;
            }



}

这是我的#Alarm; java':

public class Alarm extends Service{



public static final String MyPREFERENCES = "crntPrefs";

    private Random rand = new Random();
    int[] ids = {R.string.word1, R.string.word2 //etc....};

    SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();



    public void onStart(Intent intent, int startId) {
        {
            //my code
    }

}

logcat的:

05-17 16:54:23.048: E/AndroidRuntime(1137): FATAL EXCEPTION: main
05-17 16:54:23.048: E/AndroidRuntime(1137): Process: com.knight.spreadtheword, PID: 1137
05-17 16:54:23.048: E/AndroidRuntime(1137): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.knight.spreadtheword/com.knight.spreadtheword.MainActivity}: java.lang.IllegalArgumentException: Invalid alarm type 11
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.os.Looper.loop(Looper.java:136)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at java.lang.reflect.Method.invoke(Method.java:515)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at dalvik.system.NativeStart.main(Native Method)
05-17 16:54:23.048: E/AndroidRuntime(1137): Caused by: java.lang.IllegalArgumentException: Invalid alarm type 11
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.os.Parcel.readException(Parcel.java:1469)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.os.Parcel.readException(Parcel.java:1419)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:155)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.AlarmManager.setImpl(AlarmManager.java:363)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.AlarmManager.set(AlarmManager.java:191)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at com.knight.spreadtheword.MainActivity.onCreate(MainActivity.java:59)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.Activity.performCreate(Activity.java:5231)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-17 16:54:23.048: E/AndroidRuntime(1137):     ... 11 more

1 个答案:

答案 0 :(得分:0)

根据documentation of the AlarmManager class,类型必须是ELAPSED_REALTIME,ELAPSED_REALTIME_WAKEUP,RTC或RTC_WAKEUP之一。