有没有可靠的方法在Android中启动活动?

时间:2014-12-01 17:30:41

标签: android boot

我尝试过使用:

<action android:name="android.intent.action.BOOT_COMPLETED" />

但这是在Android平台上启动活动的一种非常不一致的方式。

启动时启动我的应用程序有什么保证吗?

2 个答案:

答案 0 :(得分:1)

您必须在清单中声明权限:

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

然后用广播接收器捕捉意图:

在清单中:

<receiver
    android:name="com.mypackagename.MyBroadcastReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

在MyBroadcastReceiver.java中:

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent i = new Intent(context, YourClass.class);
        context.startActivity(i);
    }
}

答案 1 :(得分:0)

我正在使用Android Studio。

我也在创建一个在Boot Complete上运行的服务,我最初也有一堆不一致和不可靠的结果,我的应用程序(服务)似乎甚至没有尝试启动。

有时它会完美运行,然后当我用其他一些非BroadcastReciever相关更新更新项目时,它似乎打破了自动启动。

我发现当我去运行我的应用程序并监控logcat时,我发现在某些情况下,Android OS广播服务可能需要整整30秒才能使用更新的软件包详细信息进行更新。

例如,来自logcat ...

03-03 17:08:26.757     681-1780/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=com.test.jbyrne.anotherautostart
#### Note the time stamp between the PACKAGE_REMOVED (above) and PACKAGE_ADDED (below) ####
03-03 17:08:26.825      681-681/? I/ConfigFetchService﹕ PackageReceiver: Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.test.jbyrne.anotherautostart flg=0x8000010 cmp=com.google.android.gms/.config.ConfigFetchService$PackageReceiver (has extras) }
03-03 17:08:26.833      681-681/? I/ConfigFetchService﹕ onStartCommand Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.test.jbyrne.anotherautostart cmp=com.google.android.gms/.config.ConfigFetchService (has extras) }
03-03 17:08:26.841      707-707/? D/PackageAddedReceiver﹕ package added com.test.jbyrne.anotherautostart
03-03 17:08:26.873     681-1816/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=com.test.jbyrne.anotherautostart
03-03 17:08:26.877    1419-1818/? I/UpdateIcingCorporaServi﹕ Updating corpora: APPS=com.test.jbyrne.anotherautostart, CONTACTS=MAYBE
03-03 17:08:27.005     681-1816/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=com.test.jbyrne.anotherautostart

然后我改变了一些不相关的东西,当我下次更新包裹时......

03-03 17:10:29.866    2454-3515/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=com.test.jbyrne.anotherautostart
#### This time, it takes over 30 seconds to Update the Broadcast Services!!! ####
03-03 17:11:00.190    2454-2454/? I/ConfigFetchService﹕ PackageReceiver: Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.test.jbyrne.anotherautostart flg=0x8000010 cmp=com.google.android.gms/.config.ConfigFetchService$PackageReceiver (has extras) }
03-03 17:11:00.194    2454-2454/? I/ConfigFetchService﹕ onStartCommand Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.test.jbyrne.anotherautostart cmp=com.google.android.gms/.config.ConfigFetchService (has extras) }
03-03 17:11:00.206    2404-2404/? D/PackageAddedReceiver﹕ package added com.test.jbyrne.anotherautostart
03-03 17:11:00.222    2454-3587/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=com.test.jbyrne.anotherautostart
03-03 17:11:00.246    3168-3589/? I/UpdateIcingCorporaServi﹕ Updating corpora: APPS=com.test.jbyrne.anotherautostart, CONTACTS=MAYBE
03-03 17:11:00.326    2454-3601/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=com.test.jbyrne.anotherautostart

我花了很多时间试图弄清楚为什么这么不可靠,最终归结为我在Android OS广播服务更新之前过早重启我的设备。

请等待一分钟,然后重新启动设备以测试更新,以确保更新各种广播服务。或者监视logcat以获取

android.intent.action.PACKAGE_ADDED

行。

我希望这会有所帮助!!

亲切的问候,

约翰