事件来源(SCREEN_ON)

时间:2016-10-11 06:25:28

标签: android android-intent android-broadcast

我有这样的问题: 我的应用程序订阅了Intent.SCREEN_ON事件,但在某些情况下它会产生错误的效果。我如何检测 - SCREEN_ON是由按钮(用户按下电源按钮)或任何其他(警报,来电,来自whatsapp的通知)引起的?有可能吗?

2 个答案:

答案 0 :(得分:0)

没有。这是不可能的。

每次广播的核心是Intent。如果这样的壮举是可能的,那么我们应该在其中的某个地方看到所需的信息。

但如果你看一下Intent的秘密,这些是唯一定义的领域:

    private String mAction;
    private Uri mData;
    private String mType;
    private String mPackage;
    private ComponentName mComponent;
    private int mFlags;
    private HashSet<String> mCategories;
    private Bundle mExtras;

你知道,在这些领域中,并不是真的可以告诉我们关于发件人的任何内容。

答案 1 :(得分:0)

您甚至可以屏幕显示。

MyApp的:

public class MyApp extends Application {

@Override
public void onCreate() {
    super.onCreate();
    registerKioskModeScreenOnReceiver();
}
private PowerManager.WakeLock wakeLock;
public OnScreenOnReceiver onScreenOnReceiver;




private void registerKioskModeScreenOnReceiver() {
    // register screen off receiver
    final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    onScreenOnReceiver = new OnScreenOnReceiver();
    registerReceiver(onScreenOnReceiver, filter);
}

public PowerManager.WakeLock getWakeLock() {
    if(wakeLock == null) {
        // lazy loading: first call, create wakeLock via PowerManager.
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "wakeup");
    }
    return wakeLock;
}
}

OnScreenOnReceiver接收器:

public class OnScreenOnReceiver extends BroadcastReceiver {
private static final String PREF_KIOSK_MODE = "pref_kiosk_mode";
Listener listener;
@Override
public void onReceive(Context context, Intent intent) {
    if(Intent.ACTION_SCREEN_ON.equals(intent.getAction())){

        Log.e("tag", "screeneee:" + intent.getAction());
        MyApp ctx = (MyApp) context.getApplicationContext();
        // is Kiosk Mode active?
        if(isKioskModeActive(ctx)) {
            wakeUpDevice(ctx);

        }
        if(listener!=null)
            listener.onPower();

    }
}


private void wakeUpDevice(MyApp context) {
    PowerManager.WakeLock wakeLock = context.getWakeLock(); // get WakeLock reference via AppContext
    if (wakeLock.isHeld()) {
        wakeLock.release(); // release old wake lock
    }

    // create a new wake lock...
    wakeLock.acquire();

    // ... and release again
    wakeLock.release();
}

private boolean isKioskModeActive(final Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    return sp.getBoolean(PREF_KIOSK_MODE, false);
}

public interface Listener {
    public void onPower();
}

public void setListener(Listener listener) {
    this.listener = listener;
}
}

清单添加:

<application
    android:name=".MyApp">

</application>

另一种方式:

public class MyApp extends Application {

public static boolean isActivityVisible() {
    return activityVisible;
}

public static void activityResumed() {
    activityVisible = true;
}

public static void activityPaused() {
    activityVisible = false;
}

private static boolean activityVisible = false;

 @Override
public void onCreate() {
    super.onCreate();
}
}

覆盖metod中的主要类:

@Override
protected void onResume() {
    super.onResume();
    MyApp.activityResumed();
}

@Override
protected void onPause() {
    MyApp.activityPaused();
    super.onPause();

}

onClick按钮按下:

if(MyApp.isActivityVisible()){

    }