电话睡眠时无法显示活动(闹钟)

时间:2014-07-14 10:14:15

标签: android alarmmanager broadcast receiver

我已经阅读了很多关于此问题的答案,但仍无法找到方法。

我有一个应用程序,它为会议设置提醒,并在约会前15分钟显示弹出活动,并从用户那里获得有关约会的输入。

我使用AlarmManager设置闹钟,并从广播接收器开始活动。当设备开启时工作正常,但如果是睡眠则它不会打开我的手机(但我确实设法在我的模拟器上打开锁定屏幕)。在任何一种情况下,如果没有用户手动解锁手机,我无法解锁手机并显示我的活动。

我的主要活动

public class Start extends Activity {

public static int no;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    System.out.println("On create. After content set.");
    LinearLayout l =  (LinearLayout) findViewById(R.id.startlayout);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, new GregorianCalendar().getTimeInMillis()+10000, PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReciever.class), Intent.FLAG_ACTIVITY_NEW_TASK));

}
}

我的广播传播者

public class AlarmReciever extends BroadcastReceiver {


@Override
public void onReceive(Context arg0, Intent arg1) {
    Log.i("Alarm recieved", "Executing on receive");
    PowerManager pm = (PowerManager)arg0.getSystemService(Context.POWER_SERVICE);
    WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Tag");
    wl.acquire();
    Intent i = new Intent();
    i.setClassName("com.example.attendogram", "com.example.attendogram.Reminder");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    arg0.startActivity(i);
    Log.i("Alarm recieved", "Executed on receive");
}


}

广播接收器启动的活动

public class Reminder extends Activity {

private Context context;


public Reminder() {
    // TODO Auto-generated constructor stub
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    Log.i("Reminder created", "Executing onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reminder);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    Log.i("Reminder", "Screen tunred on");
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    Log.i("Reminder", "Dismissed");
    setFinishOnTouchOutside(false);
    TextView rem = (TextView) findViewById(R.id.reminder_sna);
    rem.setText(getIntent().getStringExtra("id"));
    context = this;
}}

我的清单

<?xml version="1.0" encoding="utf-8"?>   

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.attendogram"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.attendogram.Start"
        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="SubPage"></activity>
    <activity android:name="NewSub"></activity>
    <activity android:name="Timetable" >
    </activity>
    <receiver android:name=".AlarmReciever" />
    <activity
        android:name="Reminder"
        android:theme="@android:style/Theme.DeviceDefault.Dialog.NoActionBar.MinWidth"
        android:excludeFromRecents="true" >
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

如果手机处于睡眠状态,手机将只会唤醒时间以执行广播接收器。在活动执行时,它可能会或可能不会被唤醒。

要解决此问题,您应该在Receiver中获取唤醒锁,然后启动活动...

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
               PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);

        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, App.TAG);

                wl.acquire();  
Log.i("Alarm recieved", "Executing on receive");
                Intent i = new Intent();
                i.setClassName("com.example.attendogram", "com.example.attendogram.Reminder");
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                arg0.startActivity(i);