RTC_WAKEUP is not working

杀马特。学长 韩版系。学妹 提交于 2020-01-22 10:04:10

问题


Currently i am working on a Broadcast Receiver application, in which i am making an Alarm which should display a message after we enter the seconds. I used RTC_WAKEUP, which means it should display the message when the device is on and it is supposed to turn on the device and then display the message when the device is off. MY PROBLEM IS THAT IT RTC_WAKEUP DOESN'T ON MY DEVICE but it is working properly when device is on. i am pasting the code of my application. In my application there are two classes.

MainActivity

public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 23432424, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (i * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set in " + i + " seconds",
                Toast.LENGTH_LONG).show();
    }
}

and other is

MyBroadcastReceiver

    public class MyBroadcastReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Jaago Mohan Pyarreee!!!!.",
            Toast.LENGTH_LONG).show();
      }
    } 

Manifest

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

回答1:


RTC_WAKEUP will not switch on the screen, all it does is wakes up thee cpu so that your job is done. For the Screen to be turned on you need a FULL wakelock to be acquired.

PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK
                                      | PowerManager.ON_AFTER_RELEASE,
                                      "wakeup");
 wl.acquire();
 // ... do work...
//show toask
 wl.release();



回答2:


Do yo have the permission in your Manifest?

<uses-permission android:name="android.permission.WAKE_LOCK" />



回答3:


Fix your manifest. Instead of

<receiver android:name="MyBroadcastReceiver" >

you need:

<receiver android:name=".MyBroadcastReceiver" >



回答4:


Checked the google DeskClock app: com.android.deskclock.alarms.AlarmStateManager (it's a BroadcastReceiver)

In its onReceive function used goAsync():

@Override
public void onReceive(final Context context, final Intent intent) {
    final PendingResult result = goAsync();
    final PowerManager.WakeLock wl = AlarmAlertWakeLock.createPartialWakeLock(context);
    wl.acquire();
    AsyncHandler.post(new Runnable() {
        @Override
        public void run() {
            handleIntent(context, intent);
            result.finish();
            wl.release();
        }
    });
}

You should take a shot with that.



来源:https://stackoverflow.com/questions/14398127/rtc-wakeup-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!