I am experimenting with broadcast receiver in android and I have this class as broadcast receiver:
public class BroadcastInbuilt extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Battery Low, Please Charge!", Toast.LENGTH_LONG).show();
Log.i("BroadcastPersonal","Happening");
MainActivity.b1.setText("Change text if working!!");
}
}
I have added this in Android Manifest inside application tag:
<receiver
android:name=".BroadcastInbuilt"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
There is no code in my main activity. When I change my battery level to low the toast does not appear. i am following the android tutorial and I have done exactly same as told here
https://www.youtube.com/watch?v=Uf4V5OSJji8&list=PLlyCyjh2pUe9wv-hU4my-Nen_SvXIzxGB&index=44
Result is at 9:40 in video. Now his app shows toast but mine does not.
I did some research and found out that you should not register your receiver in Android Manifest but register them dynamically through programming.
BroadcastReceiver myBroadcast = new BroadcastInbuilt(); // name of class which extends BroadcastReceiver
IntentFilter filter =new IntentFilter(BatteryManager.EXTRA_BATTERY_LOW);
filter.addAction(Intent.ACTION_BATTERY_LOW);
this.registerReceiver(myBroadcast, filter);
Be sure to unregister broadcast in onDestroy()
unregisterReceiver(myBroadcast);
Note: if receiver was registered in onCreate() then it should be unregistered in onDestroy().If it was registered in onResume() then it should be unregistered in onPause().
Related
Currently I have Broadcast Receiver for listening call states events. I have registered Broadcast Receiver in AndroidManifest.xml as shown below.
<receiver android:name=".api.PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
When Application launches this broadcast receiver is registered for listening call states events and according to CALL_STATE i am managing my application.
It is working fine until phone restarts.
After phone restart this broadcast receiver stops working. I know I have to register receiver for listening BOOT_COMPLETED event of system.
What i have done is like shown below:
<receiver android:name=".api.PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I have also given Permission for getting BOOT_COMPLETED system event.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
but somehow it is not working. I am thinking of making new Broadcast Receiver that listens for BOOT_COMPLETED event only but issue is that
So my questions is how can i start this Phone Call Listener Broadcast Receiver when any incoming call comes in?
How can i register Broadcast Receiver from another Broadcast Receiver
Do i have to move my existing broadcast receiver's code to service so i can start service from Boot Receiver?
Any help will be appreciated.
Any Other answers are welcome.
I have solved it by creating new Broadcast receiver and onReceive() method of that Broadcast receiver will be called when phone restarts then i have dynamically register READ_PHONE_STATE broadcast receiver that is also manifest registered receiver.
Below is code:
AndroidManifest.xml:
<receiver android:name=".api.ServiceStarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
BroadcastReceiver:
public class ServiceStarter extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
PhoneCallReceiver receiver = new PhoneCallReceiver();
context.getApplicationContext().registerReceiver(receiver, filter);
}
}
You have to register receiver using Application context like below:
context.getApplicationContext().registerReceiver(receiver, filter);
instead of
context.registerReceiver(receiver, filter);
Otherwise you will get following exception:
java.lang.RuntimeException: Unable to start receiver
com.ecosmob.contactpro.api.ServiceStarter:
android.content.ReceiverCallNotAllowedException: BroadcastReceiver
components are not allowed to register to receive intents
I hope it helps others!
This should be fairly easy but I somehow can't get a Broadcast receiver's onReceive method triggered. Details below:
App B provides a broadcast receiver.
Manifest:
<receiver android:name=".MyNotificationReceiver">
<intent-filter>
<action android:name="com.go.foo.A_ACTION" />
</intent-filter>
</receiver>
Java:
public class MyNotificationReceiver extends BroadcastReceiver {
private final String TAG= "MyNotificationReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "this is not shown" , Toast.LENGTH_LONG).show();
}
}
App A is Broadcast Sender App:
Java
Intent intent = new Intent();
intent.setAction("com.go.foo.A_ACTION");
sendBroadcast(intent);
Log.d(TAG, "broadcast intent sent...");
I can see the log statement that the broadcast is sent but the receiver's onReceive() callback is not getting triggered. Am I doing something wrong?
Interesting why it's not working. Try this one out. I know the default values of exported and enabled are true. But still have a try at it.
<receiver android:name=".MyNotificationReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.go.foo.A_ACTION" />
</intent-filter>
</receiver>
Learned something new today. As of Android 4.1, the system no longer sends broadcasts to app components without an activity. The broadcast received started working after I added an activity in the manifest.
I added receiver in manifest
<receiver android:name=".PackageReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
And this is my BroadcastReceiver
public class PackageReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("noti", "sucess");
//Start Notification Service
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}}
..
When I install this package, It's didn't work...
This package don't have activity.(only service and resource)
Is this a problem?
Then..
How to call BroadcastReceiver in this package without activity?
This is a question that is asked a lot.
Check out this Commonsware blog.
Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.
There needs to be an active component in your application that the user can start. When your activity gets started, your BroadcastReceiver will be registered.
You have just declared and implemented a BroadcastReceiver, but you haven't started it yet, you need an entry point to start your receiver ( activity or service )
here is the code to register
PackageReceiver packageReceiver= new PackageReceiver(this);
registerReceiver( packageReceiver, new IntentFilter("android.intent.action.PACKAGE_ADDED"));
I'm developing an Android application, i have a service running in background that i want to lauch it by introducing a custom ussd number. For example when i call #12345# my service starts.Thank you in advance
You can register a BroadcastReceiver in your manifest that listens for android.provider.Telephony.SECRET_CODE intents. You also specify the secret code in the manifest.
For example, this manifest entry registers MyBroadcastReceiver for the secret code *#*#12345#*#*.
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE"/>
<data android:scheme="android_secret_code" android:host="12345"/>
</intent-filter>
</receiver>
MyBroadcastReceiver should look something like this:
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// Create an intent to start your Service.
}
}
I want to mantain a log in my android application , log will contain the Device Started (Bootup) and Device Stop Times. Any Idea how to do this ?
I have to start my application on Bootup , But how to determine that application is started on Bootup ?
I have searched but could not find a better solution.
Use BroadCastReceiver to receive BOOT_COMPLETED broadcast. This broadcast is thrown in device startup
The receiver will be like
<receiver
android:name="ReceiverName"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
You will need to use the following persmission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
now in code write a BroadCastReceiver class like
public class ReceiverName extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// do startup tasks or start your luncher activity
}
}
You can use BroadcastReceiver component for this purpose. Using this you can detect various events of your device like booting.
To Detect Booting process you need to give permission in AndroidManifest.xml as below,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Then you need to create a BrodacastReceiver which will handle this,
In the onReceive() method the corresponding BroadcastReceiver would then start the event,
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, WordService.class);
context.startService(service);
}
}