【发布时间】:2016-03-08 10:33:29
【问题描述】:
我的应用程序基本上检测到屏幕解锁并弹出一个 toast。它可以正常工作,但是当我实现 Intent 并调用另一个活动时,它会崩溃..
package com.androidexample.screenonoff;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class AEScreenOnOffService extends Service {
BroadcastReceiver mReceiver=null;
@Override
public void onCreate() {
super.onCreate();
// Toast.makeText(getBaseContext(), "Service on create", Toast.LENGTH_SHORT).show();
// Register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new AEScreenOnOffReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = false;
try{
// Get ON/OFF values sent from receiver ( AEScreenOnOffReceiver.java )
screenOn = intent.getBooleanExtra("screen_state", false);
}catch(Exception e){}
// Toast.makeText(getBaseContext(), "Service on start :"+screenOn,
//Toast.LENGTH_SHORT).show();
if (!screenOn) {
// your code here
// Some time required to start any service
Toast.makeText(getBaseContext(), "Begin ", Toast.LENGTH_LONG).show();
我在这里添加了一个启动活动的意图。
Intent intent = new Intent(AEScreenOnOffService.this,postlockscreen.class);
startActivity(intent);
} else {
// your code here
// Some time required to stop any service to save battery consumption
//Toast.makeText(getBaseContext(), "Screen off,", Toast.LENGTH_LONG).show();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
我已经检查了新活动的 xml,该类似乎一切正常,但似乎没有任何工作。
【问题讨论】:
-
你试过挂起的意图吗??
-
向我们展示此代码发生的错误的 logcat
标签: android android-intent android-activity broadcastreceiver android-service