我一直试图制作一个简单的闹钟应用程序,但我从来没有成功让手机醒来(有或没有WakeLock)。这是我的MainActivity代码:
public class MainActivity extends AppCompatActivity {
public final int sleepRequest = 111;
public final int wakeRequest = 222;
PendingIntent pendingIntent;
AlarmManager manager;
// currently no difference between sleeping and waking alarms, nor do the alarms use the selected time
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sleepButton = (Button) findViewById(R.id.sleepButton);
sleepButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createAlarm(sleepRequest);
}
});
Button wakeButton = (Button) findViewById(R.id.wakeButton);
wakeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createAlarm(wakeRequest);
}
});
}
public void createAlarm(final int requestCode) {
showTimePicker(new Completion() {
@Override
public void onCompletion(int hour, int minute) {
setAlarm(hour, minute, requestCode);
}
});
}
public void showTimePicker(final Completion completion) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
completion.onCompletion(hourOfDay, minute);
}
}, hour, minute, false);
timePickerDialog.show();
}
public void setAlarm(int hour, int minute, int requestCode) {
Date date = new Date();
Calendar now = Calendar.getInstance();
now.setTime(date);
Calendar alarm = Calendar.getInstance();
alarm.setTime(date);
alarm.set(Calendar.HOUR_OF_DAY, hour);
alarm.set(Calendar.MINUTE, minute);
alarm.set(Calendar.SECOND,0);
if(alarm.before(now)){
alarm.add(Calendar.DATE,1);
}
manager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, requestCode, intent, 0);
manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(), pendingIntent);
}
}
对于AlarmReceiver:
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AlarmService.class);
Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
startWakefulService(context, service);
}
}
对于AlarmService:
public class AlarmService extends IntentService {
public AlarmService() {
super("AlarmService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("APP", "in onHandleIntent");
Toast.makeText(getApplicationContext(), "Hello world", Toast.LENGTH_SHORT).show();
}
}
这是AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.app">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:process=":remote"
android:name=".AlarmReceiver">
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".AlarmService" />
</application>
</manifest>
当闹钟响起时,我收到此错误:
11-07 20:52:01.106 20764-21883/com.me.app I/APP: in onHandleIntent
11-07 20:52:03.136 20764-20775/com.me.app W/MessageQueue: Handler (android.os.Handler) {f9c4a9b} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.os.Handler) {f9c4a9b} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.post(Handler.java:326)
at android.widget.Toast$TN.hide(Toast.java:738)
at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:11-07 20:52:01.106 20764-21883/com.me.app I/APP: in onHandleIntent
11-07 20:52:03.136 20764-20775/com.me.app W/MessageQueue: Handler (android.os.Handler) {f9c4a9b} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.os.Handler) {f9c4a9b} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.post(Handler.java:326)
at android.widget.Toast$TN.hide(Toast.java:738)
at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:55)
at android.os.Binder.execTransact(Binder.java:453)55)
at android.os.Binder.execTransact(Binder.java:453)
答案 0 :(得分:0)
你混淆了WakefulBroadcastReceiver所做的事情。它没有打开屏幕。它唤醒了处理器。如果你想打开屏幕,你需要使用普通的广播接收器并使用完整的唤醒锁 - WakefulBroadcastReceiver所采用的唤醒锁是部分的。