【发布时间】:2014-02-24 07:57:03
【问题描述】:
我正在尝试制作一个锁屏应用程序,这就是为什么我想在每次打开屏幕时启动我的应用程序。目前我找到了一个解决方案,其中使用了 Receiver for Screen on/off 和 onPause() 和 onResume() 方法。这是示例的链接:http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/#comment-4777
我使用的是活动示例,而不是服务。
当屏幕关闭和打开并且应用程序已经运行时,我实际上在 LogCat 中获得了反馈。问题是当我打开屏幕时应用程序没有启动(即使它显示在最近使用的应用程序下)。
我不确定,但我认为该示例运行良好,我只是缺少添加基本代码。
希望有人能帮忙!!
感谢您的快速回答。我已尝试运行您的代码,但仍然无法正常工作。我不确定出了什么问题。没有错误信息。该应用程序只是正常运行,但在屏幕出现时不会启动。
为了更容易帮助我,这里是我的代码;)
这是我的接收器:
package com.example.screenlocker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
public static boolean screenOff = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = true;
Intent i = new Intent(context,LockService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = false;
}
}
这是我的服务:
package com.example.screenlocker;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
public class LockService extends Service {
public void onCreate(){
IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver locker=new StartMyServiceAtBootReceiver();
registerReceiver(locker, filter);
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if(screenOn){
startActivity(new Intent(this, MainActivity.class));
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
也许你知道出了什么问题。
【问题讨论】:
标签: android screen lockscreen