首先先定义一个service 别忘了注册哦
package com.Hisense.service;
import java.util.List;
import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import com.Hisense.consant.Constant;
import com.Hisense.hisense_video.AutoPictureActivity;
import com.Hisense.hisense_video.AutoVideoActivity;
public class MyService extends Service {
KeyguardManager mKeyguardManager = null;
KeyguardLock mKeyguardLock = null;
int meidatype;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("mytest","mediatyoe11"+Constant.MEDIATYPE);
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock("");
mKeyguardLock.disableKeyguard();
BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d("mytest","mediatyoe"+Constant.MEDIATYPE);
try {
if(Constant.MEDIATYPE==1){
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClass(MyService.this, AutoVideoActivity.class);
context.startActivity(i);
Log.d("mytest","启动activity");
// finish();
Log.i("BroadcastReceiver","BroadcastReceiver");
}else if(Constant.MEDIATYPE==2){
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClass(MyService.this, AutoPictureActivity.class);
context.startActivity(i);
}
} catch (Exception e) {
Log.i("Output:", e.toString());
}
}
};
registerReceiver(mMasterResetReciever, new IntentFilter(
Intent.ACTION_SCREEN_OFF));
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
在次服务里注册了一个广播 ,该广播用于接受屏幕长时间无操作休眠( screen_off)的事件,所以此时屏幕已经熄灭,你得强制启动activity(就是你所写的屏保activity),让屏幕跳出你的屏保画面
这是你的屏保界面:
package com.pingbao;
//Download by http://down.liehuo.net
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
public class ZyScreenSaver extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Log.i("MyScreenSaver", "我是屏保");
}
最后只要写一个mainactivity来启动服务就行了
package com.pingbao;
//Download by http://down.liehuo.net
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class Zypingbao extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("pingbao","pingbao");
Intent mService=new Intent(Zypingbao.this,ZyScreenService.class);//启动服务
mService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(mService);
}
}
}