简体   繁体   English

如何在Android启动时启动应用程序?

[英]How to start an Application at boot up in Android?

I am trying to make an App in Titanium which launches on Startup ie as the mobile device startsup. 我正在尝试在Titanium中制作一个在启动时启动的应用程序,即在移动设备启动时启动。 I have seen code written at several places which states to do entry into the andsoid manifest file and some code like 我已经看到了在几个地方写的代码,它们声明要输入到andsoid清单文件中,并且一些代码例如

 @Override
public void onReceive(Context context, Intent intent) {
  Intent myIntent = new Intent(context, YourActivity.class);
  context.startActivity(myIntent);
 }

But i am not able to figure out that where to put this code. 但是我无法弄清楚该代码放在哪里。 In which file ?? 在哪个文件中 and where ? 在哪里?

This 2 answers will do what you need: 这2个答案将满足您的需求:

Start BroadcastReceiver after some system broadcast: https://stackoverflow.com/a/7877466/988434 在某些系统广播后启动BroadcastReceiver: https : //stackoverflow.com/a/7877466/988434

Start BroadcastReceiver on boot: https://stackoverflow.com/a/8544151/988434 在启动时启动BroadcastReceiver: https : //stackoverflow.com/a/8544151/988434

in you BroadcastReceiver you'll implement just call whatever Service/Activty you need. 在BroadcastReceiver中,您将实现所需的任何服务/活动。

There an example for that in the question for the 2 answers above. 在上面的两个答案的问题中有一个示例。

Tell me if you have any problems unanswered after reading those =]. 告诉我,如果您在阅读那些==后有任何未解决的问题。

You have to listen to the BOOT_COMPLETED intent filter. 您必须听BOOT_COMPLETED意向过滤器。 The piece of code you just quoted is from a BroadcastReceiver which will be firing up when the device will boot. 您刚刚引用的代码片段来自BroadcastReceiver ,它将在设备启动时启动。

This class has to extend from BroadcastReceiver : 此类必须从BroadcastReceiver扩展:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
          ...
    }
}

Then, you have to register that receiver in your manifest file by doing the following: 然后,您必须通过执行以下操作在该清单文件中注册该接收者:

<receiver 
    android:enabled="true"
    android:name="your_package.BootReceiverClassName"
    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>

Also you need the following permission: 您还需要以下权限:

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

By the way you have to make sure that the app is not installed on the SD Card otherwise it won't work (but there are possible workarounds ). 顺便说一句,您必须确保该应用程序未安装在SD卡上,否则它将无法正常工作(但可能有解决方法 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM