0

我想在手机解锁时启动我的应用程序。

下面是我的代码,它在手机重新启动时工作正常,但我也希望它在手机解锁时工作。

我应该怎么办?

每次用户解锁手机时,我都想启动我的应用程序。我的代码做错了什么?

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

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


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

 <application android:icon="@drawable/cherry_icon" android:label="@string/app_name">
    <activity android:name=".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>


   <receiver android:name=".BootUpReciever"   android:enabled="true" 
    android:exported="true">

 <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.SCREEN_OFF" />
    <action android:name="android.intent.action.SCREEN_ON" />                    
    <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

    </receiver>  






  <activity android:name=".ListInstalledApps" > </activity> 


 <activity android:name=".TabsLayoutActivity" />
  </application>

 </manifest>


 public class BootUpReciever extends BroadcastReceiver {  

    @Override  
    public void onReceive(Context context, Intent intent) {
        System.out.println("in broad....");

        if ((intent.getAction() != null) &&  
             (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
        {

        System.out.println("in broadcast receiver.....");
        Intent i = new Intent(context, MainActivity.class);  
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(i);  
 }

 else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

     Intent i = new Intent(context, MainActivity.class);  
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(i);
 } 

  }

}
4

2 回答 2

1

检查清单文件

<activity
        android:name="com.wallop.tech.MyAndroidAppActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"

    android:name="Startup" >

    <intent-filter android:enabled="true" android:exported="false">
        <action android:name="android.intent.action.SCREEN_ON" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
         <action android:name="android.intent.action.USER_PRESENT" />
         <action android:name="android.intent.action.SCREEN_OFF"/>
    </intent-filter>
</receiver>
于 2013-08-21T08:01:24.277 回答
1
    import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class Startup extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null) {
            if( intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context, MyAndroidAppActivity.class);
                    s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


                    context.startActivity(s);
            }
    }


}
}
于 2013-08-21T08:05:18.010 回答