I want to load my app after the phone reboots. For this I am using this in manifest file:
<receiver android:name="com.app.BootReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
and I have BootReceiver Class which is :
package com.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
It works when I run my application on an attached device with npx react-native run-android command.
But when I create a release build APK to test it on a device it doesn't work. On the same device if I attach it to computer and install the app using react-native run-android, app automatically gets started after phone restart
What am I missing here??
I want to reload my app automatically if it was already running in the background after the phone reboots
Related
I can't seem to catch this and I also can't see it being sent in Logcat. From reviewing ACTION_MY_PACKAGE_REPLACED not received, it appears MY_PACKAGE_REPLACED should be all I need for API22.
What am I missing?
Thank you.
Snippet from AndroidManifest.xml
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
BootReceiver
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "android.intent.action.BOOT_COMPLETED" ||
intent.action == "android.intent.action.MY_PACKAGE_REPLACED") {
Log.d(TAG, "Caught BOOT_COMPLETED or PACKAGE_REPLACED action!")
GlobalScope.launch(Dispatchers.IO) {
...
}
}
}
}
Upon further investigation, this works properly if Android Studio (AS) is removed from the picture; use it to build the APK and, perhaps, view Logcat, but that’s it. If I only install/replace the app from the command line/Terminal, and do not Run app and related from AS, this works as intended. In my case, since I frequently install/Run from AS, I had to do the following TWICE and android.intent.action.MY_PACKAGE_REPLACED was caught the SECOND time:
adb -s emulator-5554 install -r app-debug.apk
I repeat, running the app from Android Studio, in this regard, severally messes with android.intent.action.MY_PACKAGE_REPLACED and I, sadly, lost a couple of hours figuring this out!
I am trying to add a broadcaster to this Android clipboard service. I have not programmed Kotlin before.
The receiver portion of AndroidManifest.xml looks like this:
<receiver android:name=".MyBroadcastReceiver" android:exported="true" android:enabled="true">
<intent-filter>
<data android:scheme="eu.micer" />
</intent-filter>
<intent-filter>
<action android:name="eu.micer.BOO" />
</intent-filter>
</receiver>
The project file MyBroadcastReceiver.kt (Kotlin) that overrides onReceive for the receiver reads simply:
package eu.micer.clipboardservice
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.BroadcastReceiver
import android.widget.Toast
import android.util.Log
private const val TAG = "MyBroadcastReceiver"
class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d(TAG, "Hi there!")
}
}
I can build, and install the app fine, and can initiate its bare-bones activity:
$adb shell am start -n eu.micer.clipboardservice/eu.micer.clipboardservice.EmptyActivity
---
Starting: Intent { cmp=eu.micer.clipboardservice/.EmptyActivity }
The broadcast fails though: after I run
$ adb shell am broadcast -a eu.micer.BOO
I get
$ adb logcat BroadcastQueue:W *:S
---
...
...
W BroadcastQueue: Background execution not allowed: receiving Intent { act=eu.micer.BOO flg=0x400010 } to eu.micer.clipboardservice/.MyBroadcastReceiver
I know there was much ado about implicit broadcasters no longer being declarable in AndroidManifest.xml for sufficiently recent SDKs, but does that apply above? Given the way it's declared, does that broadcaster count as implicit or explicit?
How can I launch an Activity from another Application Module within the same project?
I am not talking about how to open an Activity from another application, therefore this is not a duplicate of Launch an application from another application on Android
I have 2 modules: mobile and tv. Both are com.android.application on their build.gradle files and both are on the same package com.example.myapp.
I want the tv module to launch the MainActivity of the mobile module.
I've added the following line to my build.gradle(tv) file:
compile project(path: ':mobile')
on MainTvActivity.java I tried to reference the MainActivity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
which didn't work, because it couldn't resolve the symbol MainActivity. It won't even build.
The other attempt was to use intent filters:
on AndroidManifest.xml(mobile)
<activity
android:name="com.example.myapp.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.example.myapp.mobile" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
on MainTvActivity.java:
Intent intent = new Intent("com.example.myapp.mobile");
startActivity(intent);
which will build and install but will crash because it won't find anything to resolve the intent. However, if I install the mobile version and then run the tv one, it will work (as expected).
I have implemented Broadcast Receiver in a library project for checking the Boot Completed event , but it is not working.
Broadcast Receiver Class :
public class Reciever extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
Toast.makeText(context, "Device Boot Completed", Toast.LENGTH_LONG).show();
}
}
}
AndroidManifest.xml :
<receiver
android:name=".Reciever"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I implemented same Receiver in another application (not library project) and it is working fine.
BroadcastReceiver cannot be defined in the library project's manifest. The hosting project always needs to declare the components.
android library project and Activities
Edit:
I feel this should work with latest Android studio/ gradle based projects.
You might skipped
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In manifest file
Override the following method
#Override
public void onReceive(Context context, Intent intent)
You should add receive tag and reboot permissions to client application manifest file which using your library jar
Like this;
<receiver android:name="com.example.library.ReceiverClass"
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I writed a small program to catch the system broadcast BOOT_COMPLETED, but it just doesn't work:
package com.alex.app.testsysreboot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("my_tag", "system reboot completed.......");
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alex.app.testsysreboot"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>
I closed the AVD, and then clicked the button "run" in Eclipse, and the Eclipse started a new AVD, but after the system boot, I just cannot see the log in the LogCat...
Well I tried this and it Works for me,
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
}
}
AndroidManifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.saltriver" android:versionCode="1" android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
You need to add
android:enabled="true"
android:exported="true"
AND
make sure that the app is not installed on the SD card - IIRC apps installed there don't receive that BOOT_COMPLETED.
Another point is that devices with "Fast Boot" enabled (like several HTC devices) (sometimes?) don't send BOOT_COMPLETED.
Since Android 3.1+ there is some more weirdness regarding BOOT_COMPLETED relating to "very first start of an app" - see http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
A working sample project with source see https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBoot
From http://arthurfmay.blogspot.com/2011/06/broadcastreceiver-bootcompleted-and.html
So instead, from Eclipse I just went into the Android SDK and AVD
Manager (under the Window Menu) and started the emulator from there. I
did this of course after loading the app into the emulator. I start
the emulator and my BroadcastReceiver on boot works just fine. There
was no need to go to running the emulator at the command line.
Another working sample can be found here.