我的代码应在按下Button1 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();
}
}
<?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>
答案 0 :(得分:0)
您已将代码发布在
中在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);
我有一个单独的功能,以便我可以设法在某些事件上更新它!
当您要求此服务时,它也取决于您。什么事情!