如何在检测到android.intent.action.USER_PRESENT时运行代码?

时间:2016-10-24 11:18:09

标签: android service broadcastreceiver single-threaded

我只需要在检测到android.intent.action.USER_PRESENT时运行代码,并且我已经创建了一个运行线程来执行此操作的服务,但这是正确的方法吗?我的意思是,当检测到android.intent.action.ACTION_SCREEN_ON时,我的服务再次启动并且什么都不做,因为运行代码的条件是android.intent.action.USER_PRESENT

这是我的服务类:

public class MyService extends Service {
private _Thread thread = new _Thread();
ExecutorService executor = Executors.newSingleThreadExecutor();
private KeyguardManager kM;
private boolean isLocked, wasLocked;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

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

    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_USER_PRESENT);
    registerReceiver(mReceiver, filter);

    kM = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {     
    isLocked = kM.inKeyguardRestrictedInputMode();

    executor.execute(thread);
    thread.running = true;

    if (!isLocked) {            
        thread.runMyCode = true;
    }
    executor.shutdown();
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

public class _Thread implements Runnable {
    private boolean running, runMyCode;

    public void run(){
        while (running) {
            try {
                Thread.sleep(500);                        
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (runMyCode){
                // Run my code or method
            }
        }          
        stopSelf();
    }
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        isLocked = kM.inKeyguardRestrictedInputMode();

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
            if (!isLocked) {
                thread.runMyCode = true;
                wasLocked = false;
            }
        }

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
            if (!wasLocked){
                thread.runMyCode = false;                    
                wasLocked = true;
            }
        }

        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){               
            wasLocked = false;
            thread.runMyCode = true;
        }
    }
};

@Override
public void onDestroy() {
    super.onDestroy();

    thread.running = false;
    unregisterReceiver(mReceiver);

    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}

}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android.servicetest">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="Service Test"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>       
    <service android:name=".MyService"></service>
    <receiver android:name="com.example.android.servicetest.BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

0 个答案:

没有答案