我的PowerManager没有按预期打开设备



我的代码应该在按下按钮1 15秒后生成一个通知。通知代码位于服务"NotificationService"中。当我的设备打开时,一切都很好,但当我锁定设备时,我希望我的代码会自动打开设备,然后生成通知。这并没有发生。让我知道我在这里做错了什么。

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".NotificationActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:text="Button1" />
</RelativeLayout>

主要活动

package com.example.notificationex;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NotificationActivity extends Activity implements OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        Button bn1 = (Button)findViewById(R.id.button1);
        NotificationManager nma = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        nma.cancel(123);
        bn1.setOnClickListener(this);
    }
    public void onClick(View v){
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent its = new Intent(this, NotificationService.class);
        PendingIntent pis = PendingIntent.getService(this, 0, its, 0);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+15*1000, pis);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.notification, menu);
        return true;
    }
}

服务

package com.example.notificationex;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.widget.Toast;
public class NotificationService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Override
    @Deprecated
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "start", Toast.LENGTH_SHORT).show();
        PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK
                                      | PowerManager.ON_AFTER_RELEASE,
                                      "wakeup");
        wl.acquire();
        Intent it = new Intent(this, NotificationActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, it, 0);
        String body = "Reach the Interview hall";
        String title = "Interview";
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification n = new Notification(R.drawable.ic_launcher,"You have a notification",System.currentTimeMillis());
        n.setLatestEventInfo(this, title, body, pi);
        n.defaults = Notification.DEFAULT_ALL;
        nm.notify(123, n);
        wl.release();
    }

}

ManifestFile

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notificationex"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notificationex.NotificationActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".NotificationService"></service>
    </application>
</manifest>

您已经在中发布了代码

onStart()

每当您从调用服务时,都会调用此函数一次

startService(意向);

所以,只要做一个处理程序或类似的东西,然后打电话给它,这样你就可以有时间锁定手机并进行检查。检查我的代码。

KeyguardManager km = (KeyguardManager) context
            .getSystemService(Context.KEYGUARD_SERVICE);
    final KeyguardManager.KeyguardLock kl = km
            .newKeyguardLock("MyKeyguardLock");
    kl.disableKeyguard();
    PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
            | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    wl.acquire(15000);

我有一个单独的功能,这样我就可以在一些事件中更新它!

这也取决于您何时调用此服务。在哪个活动上!

最新更新