我已经尝试使用setRepeating函数对AlarmManager进行半小时的休息。但是它在不稳定的时间不断给我报警。我的代码是
MainActivity.java
package com.knby.alarmmanagerdemo;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
Button b1=(Button)findViewById(R.id.startAlarmAt10);
Button b2=(Button)findViewById(R.id.stopAlarm);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
AlarmManager manager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,14);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),60*1000*30, pendingIntent);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(MainActivity.this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
});
}
}
AlarmReceiver.java
package com.knby.alarmmanagerdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Kevin on 7/17/2016.
*/
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I'm running bro!", Toast.LENGTH_SHORT).show();
Log.i("Errore","Toast?");
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.knby.alarmmanagerdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<receiver android:name=".AlarmReceiver"
>
</receiver>
</application>
<!-- Will not be called unless the application explicitly enables it -->
</manifest>
我的日志
07-23 15:19:47.770 20447-20447/com.knby.alarmmanagerdemo W/System: ClassLoader referenced unknown path: /data/app/com.knby.alarmmanagerdemo-1/lib/arm
07-23 15:19:47.977 20447-20447/com.knby.alarmmanagerdemo W/System: ClassLoader referenced unknown path: /data/app/com.knby.alarmmanagerdemo-1/lib/arm
07-23 15:19:48.146 20447-20447/com.knby.alarmmanagerdemo W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-23 15:19:48.377 20447-20485/com.knby.alarmmanagerdemo I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: (Ifd751822f5)
OpenGL ES Shader Compiler Version: XE031.06.00.05
Build Date: 01/26/16 Tue
Local Branch: AU12_SBA
Remote Branch:
Local Patches:
Reconstruct Branch:
07-23 15:19:48.379 20447-20485/com.knby.alarmmanagerdemo I/OpenGLRenderer: Initialized EGL, version 1.4
07-23 15:20:35.339 20447-20447/com.knby.alarmmanagerdemo I/Errore: Toast?
07-23 15:21:24.484 20447-20447/com.knby.alarmmanagerdemo I/Errore: Toast?
07-23 15:24:00.671 20447-20447/com.knby.alarmmanagerdemo I/Errore: Toast?
07-23 15:27:01.944 20447-20447/com.knby.alarmmanagerdemo I/Errore: Toast?
07-23 15:30:25.860 20447-20447/com.knby.alarmmanagerdemo I/Errore: Toast?