I'm working on an orange pi 2g IoT and BroadcastReceiver cannot start an activity or service on boot completed. When the app is running is can catch boot completed but it's not running as it cannot catch broadcasts.
Attached log :
07-09 22:49:26.840 509-523/system_process V/BroadcastQueue: Received BROADCAST_INTENT_MSG
processNextBroadcast [background]: 0 broadcasts, 1 ordered broadcasts
processNextBroadcast : br=BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED}
Processing ordered broadcast [background] BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED}
Submitting BROADCAST_TIMEOUT_MSG [background] for BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED} at 328627
CHECK IS Need to start app [background] com.example.b_oyu.startuptest:com.example.b_oyu.startuptest for broadcast BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED}
Skipping delivery of ordered [background] BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED} for whatever reason about :com.example.b_oyu.startuptest
Schedule broadcasts [background]: current=false
Received BROADCAST_INTENT_MSG
processNextBroadcast [background]: 0 broadcasts, 1 ordered broadcasts
processNextBroadcast : br=BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED}
and
CHECK IS Need to start app [background] com.example.ggt.helloserial:com.example.ggt.helloserial for broadcast BroadcastRecord{421b61c8 u0 android.intent.action.BOOT_COMPLETED}
Skipping delivery of ordered [background] BroadcastRecord{421b61c8 u0 android.intent.action.BOOT_COMPLETED} for whatever reason about :com.example.ggt.helloserial
Schedule broadcasts [background]: current=false
Received BROADCAST_INTENT_MSG
Manifiest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ggt.helloserial"
android:installLocation="internalOnly"
>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.example.ggt.helloserial.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.ggt.helloserial.BootCompleted"
android:label="BootCompleted"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT"/>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.HOME"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name=".MainService"></service>
</application>
</manifest>
BroadCastReceiver
public class BootCompleted extends BroadcastReceiver {
CustomNotification NotMan= new CustomNotification();
public BootCompleted() {
}
#Override
public void onReceive(Context context, Intent intent) {
final PendingResult pendingResult = goAsync();
try
{
// Thread.sleep(1000);
Intent activityIntent = new Intent(context, MainService.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("BC", "From BootCompleted");
NotMan.ShowNotification(context,"From BootCompleted:" + intent.getAction(),0 );
Log.e("BC", "From BootCompleted");
context.startService(activityIntent);
}
catch (Exception ex)
{
Log.e("BootCompletedError", ex.getMessage());
}
pendingResult.finish();
}
}
You have very less execution time in Broadcast receivers if you want to get some more time you can use goAsync() like this.
#Override
public void onReceive(final Context context, final Intent intent) {
final PendingResult pendingResult = goAsync();
//some little long running task
pendingResult.finish();
}
Check this official doc for more details.
in you case it'd look like
#Override
public void onReceive(final Context context, final Intent intent) {
final PendingResult pendingResult = goAsync();
try{
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("BC", "From BootCompled");
NotMan.ShowNotification(context,"From BootCompled:" + intent.getAction(),0 );
context.startActivity(activityIntent);
}
catch (Exception ex){Log.e("BC", ex.getMessage());}
pendingResult.finish();
}
Related
I found many topic but no case fit for my question.
The first my app working smooth, it is always startup with device by code bellow:
In AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and receiver:
<!-- start when module start broadcast -->
<receiver android:name=".StartOnBoot"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- listen messenger received -->
<receiver
android:name=".Phone.SmsListener"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And class StartOnBoot
public class StartOnBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent serviceIntent = new Intent(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
} }
And now I added 2 broadcast to listen state of SD card and SIM card. But when I finish, My app can not start when my device restarted. And I don't know, where is problem, nothing clue.
<!-- broad cast SDCard receive-->
<receiver android:name=".Peripheral.SDCardListener">
<intent-filter>
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
<receiver android:name=".Phone.SimCardReceiver">
<intent-filter>
<action android:name="android.intent.action.SIM_STATE_CHANGED"/>
</intent-filter>
</receiver>
And 2 classes receiver:
public class SDCardListener extends BroadcastReceiver {
//restart app when SD card mounted
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG","SDcard state : " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)){
//sdcard eject
}else if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
SDCard.getInstance().SDCardMounted();
}
}
}
for sim state:
public class SimCardReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "Sim state :" + intent.getAction());
}
}
My device target is only Android 7.1, SDK lv 25.
Some methods I tried but not work:
I removed SimCardReceiver
I added "android:priortity to StartBoot" like this:
<intent-filter android:priority="1000">
But still not working, Remember, my app always work without 2 broadcast SDCardListener and SimCardReceiver
Extra question:
How many broadcast are available to use in one app. How many is good ? one or not limit
Could I merge all receiver to one broadcast
I wrote my custom broadcast receiver to receive intents from my other app but it doesn't recieve anything. Im sure that first app is sending broadcast corectly. Can someone help me?
App1:
public void broadcastIntent() {
Intent intent = new Intent();
String permissions = "com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION";
intent.putExtra("name", editName.getText().toString());
intent.putExtra("price", Float.parseFloat(editPrice.getText().toString()));
intent.putExtra("quantity", Integer.parseInt(editQuantity.getText().toString()));
intent.setAction("com.example.android.projekt1.notification");
sendBroadcast(intent, permissions);
}
And I run this function on my setOnClickListener method.
There is my broadcast receiver:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intennt received.", Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context, MyService.class);
serviceIntent.putExtras(intent);
context.startService(serviceIntent);
}
}
So while sending broadcast I have 0 toast messages and my service doesn't run too.
There is androidManifest from receiver:
<?xml version="1.0" encoding="utf-8"?>
<permission-group android:name="com.example.android.mybroadcastreceiver.my_permissions"
android:label="my permissions group"/>
<permission android:name="com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION"
android:permissionGroup="com.example.android.mybroadcastreceiver.my_permissions"
android:label="my permission"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver"
android:permission="com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION">
<intent-filter>
<action android:name="com.example.android.projekt1.notification">
</action>
</intent-filter>
</receiver>
<service android:name="MyService" />
</application>
I run my receiver first then I run my main app and press the button to send broadcast
set package name of targeting application:
public void broadcastIntent() {
Intent intent = new Intent();
String permissions = "com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION";
intent.putExtra("name", editName.getText().toString());
intent.putExtra("price", Float.parseFloat(editPrice.getText().toString()));
intent.putExtra("quantity", Integer.parseInt(editQuantity.getText().toString()));
intent.setAction("com.example.android.projekt1.notification");
intent.setPackage("Package name of receiver app");// set Package of targeting app
sendBroadcast(intent, permissions);
}
I have a several devices (Minix X68-i) all running the same firmware and software versions.
On some of them my boot receiver works fine, others it rarely works, and a few it's closer to 50-50 whether they will receive the event.
I'm out of ideas as to what could cause the code to work so erratically, other than potentially faulty hardware.
Manifest:
<receiver android:enabled="true" android:exported="true"
android:name="com.proreception.displaymanagement.Receiver.BootReceiver"
android:label="StartMyActivity" >
<intent-filter android:priority="500" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BootReceiver
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = "BootReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) && constants.ONBOOT) {
Intent i = new Intent(context, FullscreenActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "Starting Application");
context.startActivity(i);
}
}
}
I registered multiple intent under one receiver
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name="com.test.callerpopup.CallerPopUp"
android:enabled="true"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.REBOOT" ></action>
<action android:name="android.intent.action.BOOT_COMPLETED" ></action>
<action android:name="android.intent.action.PHONE_STATE" ></action>
</intent-filter>
In my Broadcast receiver class
#Override
public void onReceive(final Context context, Intent intent) {
String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING) ){
Toast.makeText(context, "Incoming Call", Toast.LENGTH_LONG).show();
}
Normal execution of app is working fine but when i am restarting phone, the toast is not working . Why ? I have registered the receiver on "REBOOT" and "BOOT_COMPLETED"
When i start service via Broadcast Receiver its showing me the logcat
01-29 12:20:33.245: W/ContextImpl(4721): Implicit intents with startService are not safe: Intent { act=actions.com.sound_profile_change.MyService } android.content.ContextWrapper.startService:494 android.content.ContextWrapper.startService:494 actions.com.sound_profile_change.MyReceiver.onReceive:30
01-29 12:20:33.245: W/ActivityManager(511): Unable to start service Intent { act=actions.com.sound_profile_change.MyService } U=0: not found
In Service Side:
#Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
timerTask.cancel();
Intent intent = new Intent("This.is.Receiver");
sendBroadcast(intent);
}
In Receiver Side:
#Override
public void onReceive(Context context, Intent intent) {
if(context!=null){
System.out.println("Am in Receiver ");
System.out.println("Not Null :>>"+context);
Intent services = new Intent();
services.setAction(
"actions.com.sound_profile_change.MyService");
context.startService(services);
// context.startService(new Intent(context, MyService.class));
}else{
System.out.println("Its Null");
}
}
In Manifest File:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true" />
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="This.is.Receiver"/>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
Help Me...
"Implicit intent" warning that your are getting is because Using Implicit Intents you have not mentioned a specific component (for example: name of service class).
Implicit Intents does have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.
So, it means the component will now be chosen by Android intent-filter mechanism, by given Intent-Action.
You may try following in code while starting a service:
Intent services = new Intent(context,MyService.class);//Explicit mention of service class name while creating intent
services.setAction("actions.com.sound_profile_change.MyService");
context.startService(services);
Above code is now using "explicit intent". Explicit Intents have specified a component which provides the exact class to be run.