屏幕关闭时重新启动应用程序
当用户触摸屏幕时,我的应用程序正在完成。为此,在 onTouch() 方法中,我有
Intent intent = new Intent(getBaseContext(), FinActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
FinActivity 类:
public class FinActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new AlarmReceiver();
registerReceiver(mReceiver, filter);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (60 * 1000),
System.currentTimeMillis() + (60 * 1000), pendingIntent);
finish();
}
我想在屏幕关闭时重新启动我的应用程序。我有这个 AlarmReceiver 类:
public class AlarmReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
System.out.println("Screen OFF");
wasScreenOn = false;
Intent i = new Intent(context, SplashScreen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
System.out.println("Screen ONN");
wasScreenOn = true;
}
}
}
但 60 秒后我在这一行得到 NullPointerException : intent.getAction().equals(Intent.ACTION_SCREEN_OFF)
我的错误在哪里?我做错了什么?
提前致谢。
My app is finishing when user touch the screen. For this, on onTouch() method I have
Intent intent = new Intent(getBaseContext(), FinActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
where FinActivity class is this one :
public class FinActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new AlarmReceiver();
registerReceiver(mReceiver, filter);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (60 * 1000),
System.currentTimeMillis() + (60 * 1000), pendingIntent);
finish();
}
I want to restart my app when screen is OFF. I have this AlarmReceiver class :
public class AlarmReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
System.out.println("Screen OFF");
wasScreenOn = false;
Intent i = new Intent(context, SplashScreen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
System.out.println("Screen ONN");
wasScreenOn = true;
}
}
}
but after 60 seconds I get NullPointerException at this line : intent.getAction().equals(Intent.ACTION_SCREEN_OFF)
Where is my mistake ? What i do wrong?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想知道屏幕是打开还是关闭,您可以使用 Android 的 PowerManager 类,它来自 api level 1。您可以使用 isScreenOn ()了解屏幕状态的方法。
您可以获取更多详细信息 http://developer.android.com/reference/android/ os/PowerManager.html 此处。
If you just want to know whether your screen is turned on or off, you can use PowerManager class of android it is from api level 1.You can use isScreenOn() method for knowing status of screen.
You can get more details http://developer.android.com/reference/android/os/PowerManager.html here.