在启动时启动服务需要花费太多时间,为什么?

时间:2015-08-03 08:41:55

标签: android broadcastreceiver android-service

我正在尝试构建一个启动显示View的服务的应用。我想要做的是在启动后快速显示视图,但是花费太多时间(> 1分钟)来启动服务。 我的BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        Intent intentservice = new Intent(context,NewService.class);
        try{
        context.startService(intentservice);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        Intent intent11 = new Intent(context,MainActivity.class);
        intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
   else if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
    {      
       context.startService(new Intent(context,NewService.class));
    }

清单:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name="com.example.p.NewService"></service>
<receiver android:name="receiver.myReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.SCREEN_OFF" />
    <action android:name="android.intent.action.SCREEN_ON" />
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>    
</receiver>

怎么办?有没有办法设置优先级或类似的东西?

1 个答案:

答案 0 :(得分:2)

内部接收器替换权限:

  <intent-filter android:priority="1000">
        <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>

这应该可以加快速度。