I am learning about BroadCastReceiver. What I am trying to achieve with the following piece of code is, I would like to see a Toast when I switch to airplane mode, where the app is on or not. What am I not doing / Doing wrong? Please Help. Thanks
ConnectivityChangedReceiver.java class
public class ConnectivityChangedReceiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent ){
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kirathe.mos.c_max">
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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=".ConnectivityChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE">
</action>
</intent-filter>
</receiver>
</application>
MainActivity.java`
public class MainActivity extends AppCompatActivity {
private TextView switchStatus;
private Switch mySwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
From the android documentation :
https://developer.android.com/guide/components/broadcast-exceptions
As part of the Android 8.0 (API level 26) Background Execution Limits,
apps that target the API level 26 or higher can no longer register
broadcast receivers for implicit broadcasts in their manifest.
However, several broadcasts are currently exempted from these
limitations. Apps can continue to register listeners for the following
broadcasts, no matter what API level the apps target.
and
https://developer.android.com/distribute/best-practices/develop/target-sdk
Google Play will require that new apps target at least Android 8.0
(API level 26) from August 1, 2018, and that app updates target
Android 8.0 from November 1, 2018.
"android.intent.action.AIRPLANE_MODE" is no longer in the list of exempted broadcasts. So, register your broadcast receiver in activity rather than in AndroidManifest.
The Official document of Broadcast Receivers Says:
android:exported
Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.
Try it like this,try adding,android:exported="true":
//Add this permission too
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<receiver android:enabled="true" android:name=".ConnectivityChangedReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_AIRPLANE_MODE_CHANGED"/>
</intent-filter>
</receiver>
The Real state is determined like this:
#Override
public void onReceive(Context context, Intent intent) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if(isAirplaneModeOn){
// handle Airplane Mode on
} else {
// handle Airplane Mode off
}
}
So I just found the solution to my problem above.
I changed
<receiver android:name=".ConnectivityChangedReceiver">
to
<receiver android:name="ConnectivityChangedReceiver">
(Without the '.' at the beginning of name. Hope it helps a stranded one!
if you API level greater than 26 you should add permission to your code like that:
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val _receiver = cSmsListener() //<--you BroadCastreciever class
val intentFilter = IntentFilter()
intentFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)
getApplicationContext().registerReceiver(_receiver, intentFilter);
var myPermission: Array<String> = arrayOf(Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS)
ActivityCompat.requestPermissions(this, myPermission,1)
intent- filter is not working in the receiver tag in AndroidManifest.xml file for me. So I added everything in MainActivity.java file like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter("android.intent.action.AIRPLANE_MODE");
broadcastBattery bb = new broadcastBattery();
registerReceiver(bb, intentFilter);
}
Now everything is working fine.
You have to enable your broadcast in your manifest.xml file :
<receiver android:enabled="true" android:name=". ConnectivityChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
You need to put the receiver in the bundles package:
package my.bundles.id;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
public class ConnectivityChangedReceiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent )
{
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
You should put it in a package thats the same as the bundleId and that will let the above work. The first dot in the name field means that the Class is a member of the bundles namespace. So, since it wasnt in a package, the dot made it look in the wrong place.
Related
So I literally tried almost every trick in the book to get this thing running. BUT IN VAIN.
Which is why I just gonna put all that code here. I don't see why this shouldn't work.
No errors or nothing like that. Just the, receiver never fires up.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastmannankatta">
<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=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcastmannankatta" />
</intent-filter>
</receiver>
</application>
</manifest>
package com.example.broadcastmannankatta;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "YEAHA", Toast.LENGTH_LONG).show();
}
}
package com.example.broadcastmannankatta;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendbroadcast(View view) {
Intent bIntent = new Intent();
bIntent.setAction("com.example.broadcastmannankatta");
sendBroadcast(bIntent);
}
}
My UI got a button that fires the sendbroadcast method.
As explained in Android official documentation
Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers.
If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically). You can still use a context-registered receiver when the user is actively using your app.
So please follow instructions in the link to register to your custom receiver context-registred receiver
Don't forgot to unregister from your receiver when activity get paused or destoryed.
I tried using the sample code in this tutorial but it seems outdated and it did not work. So what changes do I have to make and to what files to have my app start automatically when Android finishes booting up?
First, you need the permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, in yourAndroidManifest.xml, define your service and listen for the BOOT_COMPLETED action:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
}
}
And now your service should be running when the phone starts up.
This is how to make an activity start running after android device reboot:
Insert this code in your AndroidManifest.xml file, within the <application> element (not within the <activity> element):
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:enabled="true"
android:exported="true"
android:name="yourpackage.yourActivityRunOnStartup"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<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>
Then create a new class yourActivityRunOnStartup (matching the android:name specified for the <receiver> element in the manifest):
package yourpackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class yourActivityRunOnStartup extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Note:
The call i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); is important because the activity is launched from a context outside the activity. Without this, the activity will not start.
Also, the values android:enabled, android:exported and android:permission in the <receiver> tag do not seem mandatory. The app receives the event without these values. See the example here.
Listen for the ACTION_BOOT_COMPLETE and do what you need to from there. There is a code snippet here.
Update:
Original link on answer is down, so based on the comments, here it is linked code, because no one would ever miss the code when the links are down.
In AndroidManifest.xml (application-part):
<receiver android:enabled="true" android:name=".BootUpReceiver"
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>
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class); //MyActivity can be anything which you want to start on bootup...
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Source: https://web.archive.org/web/20150520124552/http://www.androidsnippets.com/autostart-an-application-at-bootup
Additionally you can use an app like AutoStart if you dont want to modify the code, to launch an android application at startup: AutoStart - No root
For Android 10 there is background restrictions.
For android 10 and all version of android follow this steps to start an app after a restart or turn on mobile
Add this two permission in Android Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Add this in your application tag
<receiver
android:name=".BootReciever"
android:enabled="true"
android:exported="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>
Add this class to start activity when boot up
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, SplashActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}}
We need Draw overlay permission for android 10
so add this in your first activity
private fun requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + this.packageName)
)
startActivityForResult(intent, 232)
} else {
//Permission Granted-System will work
}
}
}
The Sean's solution didn't work for me initially (Android 4.2.2). I had to add a dummy activity to the same Android project and run the activity manually on the device at least once. Then the Sean's solution started to work and the BroadcastReceiver was notified after subsequent reboots.
For flutter user, you can create a file named MainActivityReceiver.kt in package folder. eg. android/app/src/main/kotlin/com/your_company/package.
MainActivityReceiver.kt:
package com.your_company.package
import android.content.BroadcastReceiver
import android.content.Context;
import android.content.Intent;
class MainActivityReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val i = Intent(context, MainActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i)
}
}
}
Modify your AndroidManifest.xml file refer to the first answer.
I would like to add one point in this question which I was facing for couple of days. I tried all the answers but those were not working for me. If you are using android version 5.1 please change these settings.
If you are using android version 5.1 then you have to dis-select (Restrict to launch) from app settings.
settings> app > your app > Restrict to launch (dis-select)
Another approach is to use android.intent.action.USER_PRESENT instead of android.intent.action.BOOT_COMPLETED to avoid slow downs during the boot process. But this is only true if the user has enabled the lock Screen - otherwise this intent is never broadcasted.
Reference blog - The Problem With Android’s ACTION_USER_PRESENT Intent
I'm trying to reset alarms in my app and using a receiver to get onBootCompleted. To see if the intent was received, I'm using a toast. The toast only appears if I immediately open the app. Otherwise, the toast does not appear. I looked at previous questions but almost all of them involve services, which I am not using. I am not sure if that is a part of the problem.
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
package="package.name" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:configChanges="orientation|screenSize|keyboardHidden"
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="android.intent.action.ALARM_SERVICE" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReset"android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
Receiver Class
public class AlarmReset extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Hello! Got message",
Toast.LENGTH_LONG).show();
//reset alarms etc. No service set.
}
I also tried writing the manifest receiver as
<receiver android:name=".AlarmReset" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Key points I found online were to include permissions (which I did) and to watch out for logging.
What I do not understand is why it works if I immediately (within a few seconds, otherwise the toast does not appear) start my activity but is unsuccessful otherwise. I am considering testing a few possibilities like launching the activity itself through code or using a service like most others have. I am currently testing on Android 4.4 on an actual phone.
When an app is installed, it is in a stopped state. None of its components will be activated (such as your BOOT_COMPLETED receiver) until the app is moved out of this state by being launched by the user. This is why your app doesn't work unless you launch it once.
Note that force stopping the app from Settings also moves it into this stopped state.
See this page for more details (search the page for "launch controls").
Starting with Android 3.1 all applications, upon installation, are placed in a "stopped" state.(This is the same state that the application ends up in after the user force-stops the app from the Settings application.)
While in "stopped" state, the application will not run for any reason, except by a manual launch of an activity. (Meaning no BroadcastReceviers(ACTION_PACKAGE_INSTALLED, BOOT_COMPLETED etc.) will be invoked, regardless of the event for which they have registered, until the user runs the app manually.)
But you can start a serivice for ex-
1) In your element:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
I feel the need to clarify the issue and solution, since the question was not clear (due to confidentiality issues).
The Basic Solution to my problem was just starting a service.
The Problem: I was trying to make the alarm in my class, AlarmReset, through AlarmManager. That in itself may have been an issue, but in addition, I tried to access objects that were instantiated in the MainActivity, furthering my dilemma. The reason why it worked when I opened MainActivity quickly enough, I suspect, is because I was able to instantiate the objects and set up the prerequisites for the class to directly access. I think the toast not appearing is similar to the issue.
The Solution: I set up a service class which I directed AlarmReset to. This is what I changed the AlarmReset class to:
public class AlarmReset extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Hello! Got message",
Toast.LENGTH_LONG).show();
//Pretty sure the Toast doesn't appear still.
Intent service = new Intent(context, Service.class);
context.startService(service);
}
Then my service class
public class Service extends IntentService {
private DataBaseManager database;
public Service()
{
super("Service");
}
#Override
protected void onHandleIntent(Intent intent)
{
database = new DataBaseManager(this);
Toast.makeText(this, "Hi",
Toast.LENGTH_LONG).show();
Toast.makeText(this, "Hello! Got message",
Toast.LENGTH_LONG).show();
//rest of code
}
Similarly, the text does not appear except if I am on the app immediately (I suspect it has to do with the threads).
Here, I made sure to instantiate my objects before using them (or did so after crashing).
A few possible problems that others may encounter (as I have read) are the following:
Installing in internal storage
android:installLocation="internalOnly"
and receiving permission for booting
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Some problems that people had suggested and were not very important were:
Using android:enabled and android:exported. Default values are fine
Writing the full receiver name. For me it wasn't necessary
<action android:name="android.intent.action.QUICKBOOT_POWERON" /> did not seem to do much.
Hello i would to develop a simple app without Main Activity as launcher.
I want to register a broadcast receiver which starts after reboot of device and inside OnReceive callback starts an Activity
Here my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.examples"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<receiver android:name=".AfterRebootBR" android:exported="false"
android:label="Boot Notification Receiver" 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>
<activity android:name=".MainActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
And here my Broadcast receiver
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.examples;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class AfterRebootBR extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("AfterRebootBR","***************** ON RECEIVE *********************");
Log.e("AfterRebootBR","***************** ON RECEIVE *********************");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
And finally the MainActivity
package it.examples;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
What is wrong in my code?
Thanks in advance
Francesco
my code is working..here is it...
in manifest
<receiver
android:name="com.calender.calenderevent.Reboot_Reciever"
android:enabled="true"
android:exported="true"
android:label="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
I cant see anything wrong with your code, however i have something worth to try.
Move the permission out of the application tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If its not working, simplify the receiver :
<receiver android:name=".AfterRebootBR">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Still not working? Try to add some delay, as mentioned here :
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//run your service
}
}, 10000);
Quoted from the link above :
While, I suggestion to delay several seconds, e.g., 10 seconds, before
running the (1) line, which is more stable for different phones and
services.
For example, in my case, my service is going to write sd card. If you
start your service immediately, some phones may fail because the sd
card is not ready.
Starting with android 3.1 you cannot have a broadcast receiver to get spawned by the application service manager if it has no context in active state (aka. at least an activity or service that is keeping the process in an "active state")
Excerpt from specification
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.
It does this to prevent broadcasts from background services from inadvertently or
unnecessarily launching components of stoppped applications. A background service or
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES
flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet
launched and when they are manually stopped by the user (in Manage Applications).
You need to somehow start your application, and then send it in a dormant state (but registered in the app manager). You can use a service for this.
It is strongly NOT recommended to start Activity from BroadcastReciever:
https://developer.android.com/training/run-background-service/report-status.html#ReceiveStatus
Never start an Activity in response to an incoming broadcast Intent.
In my case PackageManager.DONT_KILL_APP helped:
https://developer.android.com/training/scheduling/alarms.html#boot
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
I've experimented with MIUI 8 firmware real device Xiaomi Redmi Note 3.
My findings are:
you have to add app to autorun to enable it be fired by broadcast. I've checked it with such serious apps as Viber, WhatsApp.
I've compared with manifest settings (without enabling reciever programmatically):
<receiver
android:name=".activities.broadcastrecievers.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
I would like to write an application that is triggered when a calendar reminder occurs. I realize there is no officially documented way of doing this, but I have seen in the log that when my calendar alarm goes off on my phone (Droid X), AlertReceiver indicates that it has received an android.intent.action.EVENT_REMINDER:
01-03 11:03:00.029 D 1523 AlertReceiver onReceive: a=android.intent.action.EVENT_REMINDER Intent { act=android.intent.action.EVENT_REMINDER dat=content://com.android.calendar/129407058000 flg=0x4 cmp=com.android.calendar/.AlertReceiver (has extras) }
So, I set up a simple BroadcastReceiver:
package com.eshayne.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class CalendarTest extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
android.util.Log.i("CalendarTest", "CalendarTest.onReceive called!");
}
}
with this manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eshayne.android"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.READ_CALENDAR" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true">
<receiver android:name="com.eshayne.android.CalendarTest">
<intent-filter>
<action android:name="android.intent.action.EVENT_REMINDER" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Unfortunately, when I put this on my phone and set up a calendar event with a reminder - when the reminder alerts, I still see the AlertReceiver log entry, but not mine.
I have also read here about some system intents that require registering via code rather than in the manifest. So, I tried the following instead:
package com.eshayne.android;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
public class CalendarTestDisplay extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
android.util.Log.i("CalendarTestDisplay", "received broadcast");
}
},
new IntentFilter("android.intent.action.EVENT_REMINDER"));
}
}
with this modified manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eshayne.android"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.READ_CALENDAR" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true">
<activity android:name=".CalendarTestDisplay"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
with no better result.
Any ideas what I may be missing? Or any other ideas of how I might be able to capture calendar alarm occurrences?
Thanks,
Ethan
You need to set data scheme to "content" in the intent filter.
Using manifest, add data element inside intent-filter
<receiver android:name="com.eshayne.android.CalendarTest">
<intent-filter>
<data android:scheme="content"/> <!-- this was missing -->
<action android:name="android.intent.action.EVENT_REMINDER" />
</intent-filter>
</receiver>
Using code, add datascheme in one function call
IntentFilter filter = new IntentFilter(CalendarContract.ACTION_EVENT_REMINDER);
filter.addDataScheme("content"); // this was missing
registerReceiver(myRemindersReceiver, filter);
Well, what you're trying to do is not part of the Android SDK, mostly because the calendar is not part of the operating system.
That being said, at minimum, you will need to add a <data> element to your <intent-filter>, since the Intent has a Uri.
However, I'm reasonably certain that this will not work, since the Intent also specifically identifies a component (com.android.calendar/.AlertReceiver). AFAIK, that was in the Intent at the outset, and therefore the Intent will only be delivered to that component, ignoring all other routing rules. It's conceivable the listed component only showed up after Intent resolution, but I don't think that's how those log entries work.
It is possible to make the broadcast intent "android.intent.action.EVENT_REMINDER" work by specifying DataAuthority and DataScheme in your intent filter along with your intent action.You need to specify DataAuthority as "com.android.calendar" and DataScheme as "content".
the broadcast intent "android.intent.action.EVENT_REMINDER" only gets fired when an alarm notification needs to be posted for a reminder
set a notification for your EVENT (Eg: 10 minutes before the event) in order for the broadcast intent "android.intent.action.EVENT_REMINDER" to work