Getting started at boot

I want to run the application immediately after downloading the phone. Apparently, the application starts after loading, but it immediately crashes (just to be clear, the application works fine normally). I already read and tried different solutions ( link1 , link2 ), and actually the same code works fine with the other application that I developed. Here is the code:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bluetoothx10y" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="6" android:targetSdkVersion="15" /> <uses-feature android:name="android.hardware.usb.accessory"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name=".StartMyActivityAtBootReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="landscape" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter"> </meta-data> </activity> <activity android:name=".DeviceListActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog" android:screenOrientation="landscape" /> </application> </manifest> 

StartMyActivityAtBootReceiver.java:

  public class StartMyActivityAtBootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent myStarterIntent = new Intent(context, MainActivity.class); myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(myStarterIntent); } } } 

Could the fact that I use a lot of user rights be related?

+6
source share
6 answers

I managed to solve the problem. Inside OnCreate (), I had this code (associated with a USB connection) that caused a crash:

  act_string = getIntent().getAction(); if( -1 != act_string.indexOf("android.intent.action.MAIN")){ restorePreference(); } else if( -1 != act_string.indexOf("android.hardware.usb.action.USB_ACCESSORY_ATTACHED")){ cleanPreference(); } 

Removing this code allowed it to run after a boot problem.

+1
source
 Try this: 

1] In the AndroidManifest.xml file:

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application ... <receiver android:name=".StartMyActivityAtBootReceiver" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </application> 

2] Inside the BroadcastReciever class with the name StartMyActivityAtBootReceiver as the class name.

 @Override public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } 

It worked for me. The difference in the code is as follows:

  • android: permission = "android.permission.RECEIVE_BOOT_COMPLETED" inside the receiver.
  • included "category android: name =" android.intent.category.DEFAULT "" inside the intent filter.
  • I do not check the intent in onRecieve, since I know that the code will be executed only if its true
+16
source
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <receiver android:name=".StartMyActivityAtBootReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.QUICKBOOT_POWERON"/> </intent-filter> </receiver> 
+4
source

do it so if the condition

  if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) 
+3
source

instead

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

add also this

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

some devices like HTC will not catch BOOT_COMPLETED

+1
source

I would like to add the entire manifest file, which worked for me on oppo neo 5. And even make sure that some kind of phone requires special access to achieve bootstrapping or other special permissions. So do not forget to allow access to your application !!.

Here the code is

  <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.javacodegeeks.androidserviceonbootexample" android:installLocation="internalOnly"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="SilversithService" android:theme="@style/AppTheme"> <receiver android:name="com.javacodegeeks.androidserviceonbootexample.BroadcastReceiverOnBootComplete"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.REBOOT"/> <category android:name="android.intent.category.HOME" /> </intent-filter> </receiver> <service android:name="com.javacodegeeks.androidserviceonbootexample.AndroidServiceStartOnBoot" android:enabled="true"></service> <activity android:name="com.javacodegeeks.androidserviceonbootexample.MainActivity" android:label="SilversithService"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 
0
source

Source: https://habr.com/ru/post/949295/


All Articles