I need to send broadcast from my one application to another applicaion.. any help!
my application package are 1)com.demo.database and 2)com.demo.list
Intent themesIntent = new Intent(ThemesManager.THEMES_UPDATED);
themesIntent.putExtra("package", packageName);
ctx.sendBroadcast(themesIntent);
not working..
Edits :
<receiver android:name="com.sample.ThemesUpdatedReceiver">
<intent-filter>
<action android:name="com.sample.THEMES_UPDATED"/>
</intent-filter>
</receiver>
#Ajit: Hi, Since Android API 3.0 [API level 11], If an application has never been started even once, then it's BroadcastReceiver can't receive events.As, in your case, your app has no launcher activity, so may be it is the case that causes rejection of event.
Along with that please try using below approach:
You have passed that constant value while creating Intent object. Instead pass it in method intent.setAction();
Hope this helps.
I figured that every sent broadcast is received by all applications except when you setPackage to the sending intent for specific package broadcast.
I am not receiving broadcast because my another app is not launched(that doesn't have launcher activity).
If you're going to broadcast, it generally follows you have a sender and receiver. You've posted what looks like the sender ..
sender (where ever you're sending from):
Intent toret = new Intent();
toret.setAction("com.myapp.foo");
toret.putExtra("bar", "fizzbuzz");
sendBroadcast(toret);
receiver (in eg onResume())
IntentFilter intentFilter = new IntentFilter("com.myapp.foo");
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// ... do something with the intent
}
// register the receiver
this.registerReceiver(receiver , intentFilter);
Sender always sends, receiver needs to register to listen for the intent.
Related
This is my requirement.
I have an application which can be force kill by user. I want this application to receive broadcast from other app to execute some task even it is forced kill.
I am trying to make another app with service which will send a broadcast in event 1 mins to my first application.
My first application should receive this broadcast even it is forced kill.
This is what I am trying to do.
in First app:
BroadcastReceiver dummy = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("broadcast Received","broadcast Received");
}
};
IntentFilter filter = new IntentFilter("com.action.blockapp");
registerReceiver(dummy,filter);
In my second app.
Intent intent = new Intent("com.action.blockapp");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setPackage("com.example.myapplication2");
sendBroadcast(intent);
I am not able to receive the broadcast when my app is forced kill.
Please suggest.
My point . You need to declare revice in the service running in the background. Your specific revice declared in the manifest<receiver android:name=".service.NotifyReceiver" />
</application>
When the service is run. You can get notifications from revice like below
Intent intent1 = new Intent(context, NotifyReceiver.class);
intent1.setAction(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Hope to help you!
You have registered your BroadcastReceiver dynamically, in code. When your app is force stopped, that code is no longer running and your BroadcastReceiver no longer exists. It is no longer registered.
You will need to create a proper class that extends BroadcastReceiver and create a manuifest entry <receiver> for that with an <intent-filter> that matches the broadcast Intent you are broadcasting.
I have scheduled alarm for my application.
I have implemented broadcast receiver to be triggered once the alarm time reaches.
How to manually call broadcast receiver to execute the code inside of onReceive method without replicating the code twice.
I thought of having the code in utility singleton call and call that method by having util class instance from anywhere.
But is that any other way to call that onReceive method directly or else broadcast intent problematically.
android:exported="false" //Additional parameter of receiver when
defining in manifest file.
Another question is what is that exported parameter means. Please help me to understand this.
1. The way to launch a BroadcastReceiver manually is by calling
Intent intent = new Intent("com.myapp.mycustomaction");
sendBroadcast(intent);
where "com.myapp.mycustomaction" is the action specified for your BroadcastReceiver in the manifest. This can be called from an Activity or a Service.
2. It is known that Android allows applications to use components of other applications. In this way, Activitys, Services, BroadcastReceivers and ContentProviders of my application can be started by external applications, provided that the attribute android:exported = true is set in the manifest. If it is set to android:exported = false, then this component cannot be started by an external application. See here.
Here is a more type-safe solution:
AndroidManifest.xml:
<receiver android:name=".CustomBroadcastReceiver" />
CustomBroadcastReceiver.java
public class CustomBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// do work
}
}
*.java
Intent i = new Intent(context, CustomBroadcastReceiver.class);
context.sendBroadcast(i);
You need to mention the action which is required to be filter by Android OS to notify you.
i.e.:
inside manifest file,
<receiver
android:name="com.example.MyReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.alarm.notifier" />//this should be unique string as action
</intent-filter>
and
whenever you want to call broadcast receiver's onReceive method,
Intent intent = new Intent();
intent.setAction("com.example.alarm.notifier");
sendBroadcast(intent);
How to manually call broadcast receiver to execute the code inside of
onReceive method without replicating the code twice.
Fire BroadcastReceiver using sendBroadcast same action which added in AndroidManifest.xml :
Intent intent=new Intent(CUSTOM_ACTION_STRING);
// Add data in Intent using intent.putExtra if any required to pass
sendBroadcast(intent);
what is that android:exported parameter means
As in android:exported doc : Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not
Means if:
android:exported=true: other application also able to fire this broadcast receiver using action
android:exported=false: other application not able to fire this broadcast receiver using action
Hi I am developing android application in which I am defining one broadcast receiver.I am calling receiver from my activity. I am defining broadcast receiver like this :
public class MyScheduleReceiver extends BroadcastReceiver {
private static final long REPEAT_TIME = 100 * 5;
#Override
public void onReceive(Context context, Intent intent) {
Log.i("RRRRRRRRRRRRRRRRRRRRRRRR", "on receive");
}
}
In android manifest file I am defining like this:
<receiver android:name="abc.xyz.MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and in main activity I am calling my broadcast receiver like this :
//in activity oncreate
startService(new Intent(this, MyScheduleReceiver.class));
My problem is that when call start service it's not starting my service actually. But when i restart my device it start my service because I gave intent filter "BOOT_COMPLETED". what I wanted to do actually when i call start service my service must be start,
Am I doing something wrong. How to solve this problem?
Actual what happens here is that you can staring a broadcast receiver while starting the activity and this broadcast receiver starts listening BOOT_COMPLEATED is happening or not. When this happens it comes to onreceive . If you need to start a process doing in background you can use a a Service insted of BroadcastReciever. BroadcastRecievers are used to listen for some events to happen.Go through this, it will help you
Services
BroadcastReceiver
You're either confused, or you aren't wording your question well. What you have in your manifest (and how Android works generally) is that when BOOT_COMPLETED occurs, it will call that BroadcastReceiver you defined. It will not automatically start an activity or service. If you want to do that, you need to call startService or startActivity in your onReceive function of the receiver.
You do not start BroadcastReceivers. You start services, which are long term background processes. You register BroadcastReceivers to be informed of special events (like BOOT_COMPLETED). When one of the events you registered for occurs, it will create an instance of that class and call its onReceive.
Hopefully that clears things up. If not, I suggest you reread some tutorials on services and broadcast receivers, you seem to have the two confused.
startService call would only start a Service. MyScheduleReceiver here is a braodcast receiver. To trigger broadcast receivers, you generally have to send broadcasts and not call the startService.
to start broadcasts you need to send broadcasts not startService()
add this instead of startService(new Intent(this, MyScheduleReceiver.class));
Intent intent = new Intent();
intent.setAction("pakagename.MyScheduleReceiver");
sendBroadcast(intent);
I hope it helps.
I am trying to get my app automatically launched when the phone gets connected to wifi. Here's the code I am using to both set the Broadcast receiver and to specify that once the broadcast is received I want the "Connected" activity to be launched:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
receiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent){
Intent intent2 = new Intent(getApplicationContext(),com.example.package.Connected.class);
startActivity(intent2);
}
};
registerReceiver(receiver,intentFilter);
Unfortunately it's not working. The logcat says my activity has "leaked IntentReceiver".
Does anyone know how I can solve this?
EDIT: I also tried to register the receiver via the Manifest file. I added this code to the manifest:
<receiver android:name="com.example.package.receiver">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
And then this code to my main activity:
private BroadcastReceiver receiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent){
Intent intent2 = new Intent(getApplicationContext(),com.example.package.Connected.class);
context.startActivity(intent2);
}
};
But now my app crashes once the phone connects to wifi. Logcat says "RuntimeException: Unable to instantiate receiver".
Any ideas how to solve it?
I am trying to get my app automatically launched when the phone gets connected to wifi.
Register your BroadcastReceiver in the manifest, using a <receiver> element, and have the receiver call startActivity() on the Context supplied in the onReceive() method.
Note that users may not appreciate your popping up an activity just because the device connected to WiFi.
as Per link and Activity has leaked IntentReceiver
Unregister the Broadcast Receiver that you created in the onCreate()
In the onRestart() re-register a brand new Broadcast Receiver.
I am building an Android app that works by providing information via SMS to users.Users send an SMS to a predetermined number hardcoded in the APP and in turn receive a response.
To achieve this, I am using a broadcast Receiver in my Android Manifest
<receiver android:name=".SMSReciever">
<intent-filter android:priority="2">
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
If I create a new class SMSReciever, then when a response is received, I would to somehow send the information back to the activity which the user has open on the screen. Is it possible I can do this in my main activity dynamically so that the app can be made more interactive?
I have learnt about the Context.registerReciever method, but how do I mention the priority for the receiver? If the receiver does not have a higher priority, then the messaging app might stop the broadcast from reaching my app. Has anyone come across a solution for something like this?
I used the following code to achieve what I was looking for!
IntentFilter fp = new IntentFilter();
fp.addAction("android.provider.Telephony.SMS_RECEIVED");
fp.setPriority(18);
//--- when the sms is received ---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
//Processing for received broadcast happens here
}
}, fp);
I haven't tested this myself, but I've made a quick search via Android source code.
The priority is parsed and saved into IntentFilter (see code). So you can create IntentFilter and use its setPriority() function to set priority of your broadcast receiver.
IntentFilter filter = new IntentFilter(...);
filter.setPriority(receiverPriority);// <-----
context.registerReceiver(filter, mBroadcastReceiver);
I would solve this by using a custom intent action like com.example.intent.VERIFICATION_RECEIVED and the broadcasting this from SMSReciever and then having your Activity listen for it with a BroadcastReceiver.
IntentFilter filter = new IntentFilter(PageFragment.ACTION_LOADING_DONE);
registerReceiver(new MyReceiver(), filter);
Using this approach you can keep your SMS handling in SMSReciever and your Activity doesn't need to care about how to handle a SMS.