【问题标题】:Broadcast receiver android.intent.action.BOOT_COMPLETED not working [duplicate]广播接收器android.intent.action.BOOT_COMPLETED不工作[重复]
【发布时间】:2016-11-04 10:42:48
【问题描述】:

我正在尝试在启动完成时启动服务,经过大量挖掘后,我什么也没找到。

我尝试过的第一个方法是this,我在大多数答案中都找到了这种方法,但它不起作用。

然后我发现这种方法不适用于here的3.1+

我也看过这个,https://developer.android.com/about/versions/android-3.1.html#launchcontrols

并在我的代码中也使用了 FLAG_INCLUDE_STOPPED_PACKAGES。我已经从 here 引用了这种方法。

AndroidMenifest

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

<application
    android:allowBackup="true"
    android:icon="@drawable/security"
    android:label="@string/app_name"
    android:supportsRtl="true">

    <receiver android:name="com.reversebits.projects.app.easyerase.receiver.BootReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:enabled="true"
        android:exported="true">

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

        <intent-filter>
            <action android:name="myReceiver" />
        </intent-filter>

    </receiver>

    <activity
        android:name=".Home"
        android:theme="@style/AppTheme">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <service
        android:name=".services.BootService"
        android:enabled="true">

        <intent-filter>
            <action android:name="com.reversebits.projects.app.easyerase.services.BootService"/>
        </intent-filter>

    </service>

</application>

引导接收器

public class BootReceiver extends BroadcastReceiver {

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

        if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
        {
            context.startService(new Intent(context, BootService.class));
        }
    }
}

引导服务

public class BootService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful Recommended by google instead of onStart()
        return Service.START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        loopThread();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    void loopThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                switch (am.getRingerMode()) {
                    case AudioManager.RINGER_MODE_SILENT:
                        Log.e("MyApp", "Silent mode");
                        break;
                    case AudioManager.RINGER_MODE_VIBRATE:
                        Log.e("MyApp", "Vibrate mode");
                        break;
                    case AudioManager.RINGER_MODE_NORMAL:
                        Log.e("MyApp", "Normal mode");
                        break;

                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                loopThread();
            }
        }).start();
    }
}

代码运行良好,当服务从活动开始时我得到了所有日志,但在启动完成时,它不工作

注意: 我已经阅读了几乎所有相关的答案和一些答案,我也阅读了 cmets 并且我知道几乎所有相关的东西,所以请不要向我提供其他链接,因为已经看到了大多数链接,我需要完美的解决方案。任何帮助表示赞赏。

【问题讨论】:

  • 你可以尝试添加 另一个动作
  • 好的@NayanSrivastava
  • 仍然无法工作@NayanSrivastava
  • 你检查权限了吗?
  • 只是一个建议。将来,如果您不想投反对票,请不要告诉人们“在投反对票之前阅读所有内容”。不要写“我所有的代码都可以完美运行,除了......”。不要写“我已阅读所有答案和 cmets,因此请不要发布指向它们的链接”。这让你给人一种傲慢的无所不知的印象,但既然你有一个问题想让我们帮你解决,你显然并不知道这一切。所以以后在问问题的时候,尽量保持谦虚。它将为您节省一些反对票。

标签: android broadcastreceiver android-service android-broadcastreceiver


【解决方案1】:
  <receiver android:name=".BootCompletedIntentReceiver"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service android:name=".Service"
        android:enabled="true"
        android:exported="true">

    </service>

          public class BootCompletedIntentReceiver extends BroadcastReceiver {
   public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent pushIntent = new Intent(context, Service.class);

        context.startService(pushIntent);}

别忘了

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

服务

public class BootService extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStart(Intent intent, int startId) {
    //TODO do something useful Recommended by google instead of onStart()
     new Thread(new Runnable() {
        @Override
        public void run() {
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            switch (am.getRingerMode()) {
                case AudioManager.RINGER_MODE_SILENT:
                    Log.e("MyApp", "Silent mode");
                    break;
                case AudioManager.RINGER_MODE_VIBRATE:
                    Log.e("MyApp", "Vibrate mode");
                    break;
                case AudioManager.RINGER_MODE_NORMAL:
                    Log.e("MyApp", "Normal mode");
                    break;

            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }).start();
}




@Override
public void onDestroy() {
    super.onDestroy();
}
  }

【讨论】:

  • 这根本不起作用,首先尝试过@Dr.Easy
  • 这两天我创建了一个服务和接收器,效果很好:O
  • 我想帮助你对我投反对票
  • 像我在我的应用程序中所做的那样将线程放入 onStart 中,但使用 try and catch
  • 你能告诉我代码在哪里吗? @Dr.Easy
【解决方案2】:

在清单文件中:

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

<receiver android:name=".BootCompletedReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

<service android:name="NotifyingDailyService" >
</service>

BootCompletedReceiver 类

public class BootCompletedReceiver extends BroadcastReceiver {

  @Override
 public void onReceive(Context context, Intent arg1) {
  // TODO Auto-generated method stub
   Log.w("boot_broadcast_poc", "starting service...");
   context.startService(new Intent(context, NotifyingDailyService.class));
   }

  }

服务类

public class NotifyingDailyService extends Service {

  @Override
  public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
  }

   @Override
     public int onStartCommand(Intent pIntent, int flags, int startId) {
    // TODO Auto-generated method stub
     Toast.makeText(this, "NotifyingDailyService",     
      Toast.LENGTH_LONG).show();

      Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");

     return super.onStartCommand(pIntent, flags, startId);
      }
     }

【讨论】:

  • 我试过这种方法,没用@Rushi
  • 你能在不同的设备上测试同一个应用吗?
  • 好吧,去吧..@RushiAyyappa
  • 已经在三星 Gt-7262 上试过了,还是没有得到任何东西@RushiAyyappa
  • 这看起来很奇怪。代码都很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-03
相关资源
最近更新 更多