在我的应用程序中,我没有任何UI部分,因此我需要在Device上安装Applicaton后立即启动服务.我看到很多链接,答案是不可能的,但我想这肯定是可能的.只需查看符合我要求的Android Market上的PlanB应用程序即可.下面是我的Manifest文件我是如何尝试的,但根本没有调用该服务.所以,让我知道在应用程序安装时启动服务的最佳方法是什么.
UPDATE
我也试过使用android.intent.action.PACKAGE_ADDED它可以很好地检测其他应用程序的包,但不适用于自己.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.auto.start"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="@drawable/ic_launcher" >
<service android:name=".MyService">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
<receiver android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud) 我的应用程序中的服务需要一直在后台运行,在所有设备中它的工作正常除了小米,我已经阅读了一些我们需要在应用程序的设置中启用自动启动以保持服务运行的地方.
所以请告诉我如何以编程方式启用自动启动,因为用户永远不会这样做.
任何帮助将不胜感激.
我找到了xiaomi,honor和letv的代码,但我想为联想做同样的事情
if(Build.BRAND.equalsIgnoreCase("xiaomi") ){
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}else if(Build.BRAND.equalsIgnoreCase("Letv")){
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
startActivity(intent);
}
else if(Build.BRAND.equalsIgnoreCase("Honor")){
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)