我想在屏幕解锁后和启动时立即启动一个应用程序。这怎么可能?我需要做出哪些改变?
最佳答案
要在屏幕锁定时启动您的应用程序,您需要注册一个 BroadcastReceiver有关 Intent.ACTION_SCREEN_OFF Intent 的更多信息,请查看示例 http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
要在启动时启动您的应用程序,您需要一个广播接收器来监听 BOOT_COMPLETED 操作,请参阅 How to start an Application on startup?
首先,您需要 list 中的许可:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
此外,在您的 list 中,定义您的服务并监听启动完成的操作:
<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);
}
}
}
现在您的服务应该会在手机启动时运行。
关于android - 使用屏幕锁定调用应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9653386/