2012-03-11 171 views

回答

2
  1. 为屏幕锁定时,您需要为Intent.ACTION_SCREEN_OFF意图获取更多信息注册BroadcastReceiver检查例子http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

  2. 要在引导时启动应用程序启动应用程序,你需要监听的BOOT_COMPLETED行动的广播接收器,请参阅How to start an Application on startup?

首先,你需要的许可,您的清单:

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

此外,在您的清单,定义服务和监听引导完成的动作:

<service android:name=".MyService" android:label="My Service"> 
    <intent-filter> 
     <action android:name="com.myapp.MyService" /> 
    </intent-filter> 
</service> 

<receiver 
    android:name=".receiver.StartMyServiceAtBootReceiver" 
    android:enabled="true" 
    android:exported="true" 
    android:label="StartMyServiceAtBootReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

然后,你需要定义接收器,将得到的BOOT_COMPLETED行动,并开始为您服务。

public class StartMyServiceAtBootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
      Intent serviceIntent = new Intent("com.myapp.MySystemService"); 
      context.startService(serviceIntent); 
     } 
    } 
} 

现在你的服务应该在手机启动时运行。

相关问题