如何在我的本机应用程序中接收启动完成的广播?

时间:2019-05-29 03:55:44

标签: android react-native bootcompleted

我希望我的rn应用在启动时自动启动,但看来我的应用无法接收到启动完成广播。我的代码有什么问题吗?

我已经添加了所有用户权限(uses-permission和action),并确保我的apk不在sdcard中,不在停止状态...

这是我的代码:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.rnproject"
  android:installLocation="internalOnly"
  android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
  >

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

  <application>
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
      android:windowSoftInputMode="adjustResize">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    <receiver
      android:name=".com.rnproject.BootBroadcastReceiver" 
      android:enabled="true" 
      android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED"/>
            <category android:name="android.intent.category.HOME" />
        </intent-filter>  
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED"/>
            <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
            <data android:scheme="file">
            </data>
        </intent-filter>        
    </receiver>   
  </application>
</manifest>

BootBroadcastReceiver.java


package com.rnproject; 

import android.content.BroadcastReceiver;  

import android.content.Context;  

import android.content.Intent;  

import android.util.Log;

public class BootBroadcastReceiver extends BroadcastReceiver {

    static final String ACTION = "android.intent.action.BOOT_COMPLETED";

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

        Log.d("BootBroadcastReceiver", intent.getAction()); 

        if (intent.getAction().equals(ACTION)) {    
            Intent activityIntent = new Intent(context, MainActivity.class);    

            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(activityIntent);
        }    
    }

}

MainActivity.java

package com.rnproject;

public class MainActivity {

  public static void main(String[], args) {

    System.out.print("HelloWorld");

  } 

}

如果您能解决我的问题,我希望我的应用可以接收系统广播,

0 个答案:

没有答案