清除缓存不起作用后,从主屏幕单击两次电源按钮

时间:2014-07-03 14:40:00

标签: android

我正在构建一个应用程序,该应用程序将在两次单击电源按钮后激活。打开应用程序后,当我这样做时,事情进展顺利。但是当从主屏幕试图这样做时。 Toast已激活,但也应打开的活动未打开。而且当最近的应用程序清除时,实际上没有任何工作。 以下是broadcastreceiver文件。

public class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {

public static boolean wasScreenOn = true;
static long prevTime=0;
static long currTime=0;

@Override
public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
    // do whatever you need to do here
    prevTime = System.currentTimeMillis();
    Log.d("CHECK IN RECIVER WHEN ON","CHECK IN RECIVER WHEN ON");
    wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
    // and do whatever you need to do here
    Log.d("CHECK IN RECIVER WHEN ON","CHECK IN RECIVER WHEN OFF");
    currTime = System.currentTimeMillis();
    wasScreenOn = true;
}
if ((currTime - prevTime) < 1000 && (currTime - prevTime)>-1000 ) {
    if ((currTime - prevTime) < 1000 ) {
        Toast.makeText(context, "double Clicked power button",
                Toast.LENGTH_LONG).show();
        Intent main=new Intent();
        main.setClassName("com.example.speechto", "com.example.speechto.MainActivity");
        main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(main);
        Log.e("eciver ", "double Clicked power button");
        currTime = 0;
        prevTime = 0;
    }
 }
}


}

并且清单文件包含          

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.projpb1.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.projpb1"
        android:label="@string/title_activity_new" >
    </activity>
   <receiver android:name="com.example.projpb1.CloseSystemDialogsIntentReceiver">
    <intent-filter>
        <action android:name="android.intent.action.SCREEN_OFF"></action>
        <action android:name="android.intent.action.SCREEN_ON"></action>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED">   
</action>
         <action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
    </intent-filter>
</receiver> 

</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

您无法在清单中为接收者注册SCREEN_OFF或SCREEN_ON意图;您必须通过创建意图过滤器并在运行时在活动或服务中注册接收器来动态注册它们。

            // Add this to your Activity or Service
            // Register a receiver for the screen state change
            if (screenReceiver == null){
                IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
                filter.addAction(Intent.ACTION_SCREEN_OFF);
                screenReceiver = new CloseSystemDialogsIntentReceiver();
                registerReceiver(screenReceiver, filter);
            }