我不知道如何在android模拟器完成启动后自动启动android应用程序。有没有人有任何代码片段可以帮助我?如何自动启动Android应用程序?
回答
如果通过自动启动,你的意思是在电话启动时自动启动,那么你应该注册一个BroadcastReceiver的BOOT_COMPLETED
意图。一旦启动完成,Android系统会广播该意图。
一旦你收到这个意图,你就可以启动一个可以做任何你想做的事情的服务。
请注意,虽然在手机上运行的服务始终是一个糟糕的主意,因为它即使在闲置时也会占用系统资源。您只应在需要时启动您的服务/应用程序,然后在不需要时停止它。
您必须添加一个清单权限项:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
(当然你应该列出你的应用程序使用的所有其他权限)。
然后,实现BroadcastReceiver类,它应该是简单且快速的可执行文件。最好的方法是在这个接收器中设置一个警报来唤醒你的服务(如果没有必要保持它的运行,就像Prahast写的那样)。
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
}}
然后,添加一个接收器类的清单文件:
<receiver android:enabled="true" android:name=".receivers.BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
间隔是多少? – asmgx 2017-01-22 12:33:45
编辑您的AndroidManifest.xml
添加RECEIVE_BOOT_COMPLETED
许可
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
编辑您的AndroidManifest.xml
应用部分低于权限
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
现写在下面的Activity中。
public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
我总是进来这里,为这个话题。我将把我的代码放在这里,以便我(或其他人)下次可以使用它。 (憎恨搜索我的存储库代码)。
添加权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
添加接收机和服务:
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name="Launcher" />
创建类启动:
public class Launcher extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new AsyncTask<Service, Void, Service>() {
@Override
protected Service doInBackground(Service... params) {
Service service = params[0];
PackageManager pm = service.getPackageManager();
try {
Intent target = pm.getLaunchIntentForPackage("your.package.id");
if (target != null) {
service.startActivity(target);
synchronized (this) {
wait(3000);
}
} else {
throw new ActivityNotFoundException();
}
} catch (ActivityNotFoundException | InterruptedException ignored) {
}
return service;
}
@Override
protected void onPostExecute(Service service) {
service.stopSelf();
}
}.execute(this);
return START_STICKY;
}
}
创建BootUpReceiver
类机器人重新启动后做动作。
例如推出MainActivity:
public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent target = new Intent(context, MainActivity.class);
target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(target);
}
}
- 1. Android应用程序自动启动
- 2. Android自动启动应用程序
- 3. android自动启动应用程序
- 4. Android自动启动活动(应用程序启动两次)
- 5. 当启动完成时自动启动应用程序android
- 6. 应用程序启动时自动启动Android服务
- 7. 如何在应用程序启动时自动启动Delayed_Job?
- 8. 如何在Windows启动时自动启动应用程序?
- 9. 如何在启动后自动启动Bada 2.0应用程序?
- 10. Android应用程序启动
- 11. 启动Android应用程序
- 12. 如何在应用程序启动时“自动”启用GPS?
- 13. 自动阻止应用程序从启动程序启动
- 14. 如何在Android应用程序启动时启动服务?
- 15. 如何在应用程序启动时启动Android服务?
- 16. 如何启动后启动Android应用程序作为服务
- 17. 如何在手机启动时启动Android应用程序?
- 18. 如何从Java Web应用程序启动Android应用程序?
- 19. 如何从我自己的android应用程序启动电报应用程序?
- 20. 自动启动/卸载应用程序
- 21. 自动启动ASP.NET应用程序
- 22. 自动启动弹簧应用程序
- 23. 自动启动应用程序
- 24. Android,使用parse.com推送通知,自动启动应用程序
- 25. 如何检测应用程序启动类型(自动启动还是手动)?
- 26. 如何禁用所有自动启动应用程序
- 27. 如何在应用程序自动启用时自动启用应用程序权限获取系统警报
- 28. Android应用程序启动过程
- 29. 使一个Android应用程序自动启动
- 30. Android:自动启动应用程序和加载首选项
@AdamC你错了 - 看到Prashast的答案。 – 2009-06-29 05:42:54
@Rajapandian业主应该接受一个答案,如果它是一个工作解决方案,或者在评论中提及您的期望。这对其他人会有帮助。 – naveejr 2014-07-24 05:18:52
http:// karanbalkar。com/2014/01/autostart-application-at-bootup-in-android/ – 2015-01-29 09:19:34