2009-06-29 53 views
35

我不知道如何在android模拟器完成启动后自动启动android应用程序。有没有人有任何代码片段可以帮助我?如何自动启动Android应用程序?

+0

@AdamC你错了 - 看到Prashast的答案。 – 2009-06-29 05:42:54

+0

@Rajapandian业主应该接受一个答案,如果它是一个工作解决方案,或者在评论中提及您的期望。这对其他人会有帮助。 – naveejr 2014-07-24 05:18:52

+0

http:// karanbalkar。com/2014/01/autostart-application-at-bootup-in-android/ – 2015-01-29 09:19:34

回答

12

如果通过自动启动,你的意思是在电话启动时自动启动,那么你应该注册一个BroadcastReceiver的BOOT_COMPLETED意图。一旦启动完成,Android系统会广播该意图。

一旦你收到这个意图,你就可以启动一个可以做任何你想做的事情的服务。

请注意,虽然在手机上运行的服务始终是一个糟糕的主意,因为它即使在闲置时也会占用系统资源。您只应在需要时启动您的服务/应用程序,然后在不需要时停止它。

36

您必须添加一个清单权限项:

<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> 
+0

间隔是多少? – asmgx 2017-01-22 12:33:45

16

编辑您的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); 
} 
} 
0

我总是进来这里,为这个话题。我将把我的代码放在这里,以便我(或其他人)下次可以使用它。 (憎恨搜索我的存储库代码)。

添加权限:

<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); 
    } 
} 
相关问题