I have an application that use parse push notification service. Here is the class that I'm using for receiving notification:
public class MessageReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...some code in here
}
And I also register this custom receiver in my manifest:
<receiver
android:name="com.package.MessageReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
Everything is fine with notification system. After a notification have received, I want to update the running activity UI accordingly (like showing an icon for new notification) but I don't have access to the activity object in onReceive method. What is the best practice to do that? I couldn't use the context object in this matter.
Thanks
I believe you can accomplish this by using a BroadcastReceiver.
You would define the receiver in the activities you want to have access to,
See snippet below.
public MyActivity extends Activity
{
//... code
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//... update ui here
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction("suitablename");
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
//... code
}
Then in your custom receiver send the broadcast.
public class MessageReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...some code in here
Intent intent = new Intent();
intent.setAction("suitablename");
context.sendBroadcast(intent);
}
}
Don't forget to update the manifest
<activity
android:name=".MyActivity" >
<intent-filter>
<action android:name="suitablename"></action>
</intent-filter>
</activity>
Related
I have App A and App B. In App A I want to send broadcast to App B.
This is the code for App A:
final Intent intent = new Intent();
intent.setAction("com.pkg.perform.Ruby");
intent.putExtra("KeyName", "code1id");
intent.setComponent(new ComponentName("com.pkg.AppB", "com.pkg.AppB.MainActivity"));
sendBroadcast(intent);
And in App B - In MainActivity, I have MyBroadCastReceiver Class.
public class MainActivity extends Activity {
private MyBroadcastReceiver MyReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Receive broadcast from External App
IntentFilter intentFilter = new IntentFilter("com.pkg.perform.Ruby");
MyReceiver = new MyBroadcastReceiver();
if(intentFilter != null)
{
registerReceiver(MyReceiver, intentFilter);
}
}
public class MyBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "Data Received from External App", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(MyReceiver != null)
unregisterReceiver(MyReceiver);
}
}
I am getting the error - Receiver is not registered.
First thing first declare the receiver in app B in the manifest file like this:
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.pkg.perform.Ruby" />
</intent-filter>
</receiver>
when sending the broadcast add FLAG_INCLUDE_STOPPED_PACKAGES flag to the intent [src] because when you broadcast from app A to app B , app B might not be running, this flag insures that the broadcast reachs out even apps not running:
FLAG_INCLUDE_STOPPED_PACKAGES flag is added to the intent before it
is sent to indicate that the intent is to be allowed to start a
component of a stopped application.
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
In your case it will be like this:
final Intent intent=new Intent();
intent.setAction("com.pkg.perform.Ruby");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(
new ComponentName("com.pkg.AppB","com.pkg.AppB.MyBroadcastReceiver"));
sendBroadcast(intent);
In App A: Send the broadcast here.
final Intent i= new Intent();
i.putExtra("data", "Some data");
i.setAction("com.pkg.perform.Ruby");
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
getApplicationContext().sendBroadcast(i);
In App B manifest
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.pkg.perform.Ruby" />
</intent-filter>
</receiver>
In App B MainActivity: register the receiver oncreate(), and unregister onDestroy()
public class MainActivity extends AppCompatActivity
{
private MyBroadcastReceiver MyReceiver;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter("com.pkg.perform.Ruby");
if(intentFilter != null)
{
registerReceiver(MyReceiver, intentFilter);
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
if(MyReceiver != null)
unregisterReceiver(MyReceiver);
}
}
In App B BroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String data = intent.getStringExtra("data");
Log.i("BR" ,"Data received: " + data);
}
}
There may be two cases :
Your appB is not running, hence the activity is not instantiated, and so the receiver is not registered.
Your activity is destroyed, means that you have unregistered your receiver that you registered via registerReceiver() in onCreate()
Solution :
Register your broadcast receiver in manifest.
Inside manifest of appB :
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.pkg.perform.Ruby" />
</intent-filter>
</receiver>
And comment out the line in appA
intent.setComponent(new ComponentName("com.pkg.AppB","com.pkg.AppB.MainActivity"));
Write the logic in MyBroadcastReceiver to display relevant data/launch new activity
MyReceiver is class not object. Create
myReceiver = new MyReceiver();
and put...........
registerReceiver(myReceiver,intentFilter);
If this helps some one and it works for me
In App A in activity or in a content provider-
Intent intent = new Intent("Updated");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent (new
ComponentName "com.exam.appA",
"com.exam.appA.DbaseChanged"));
getContext().sendBroadcast(intent);
In App B in the manifest
<receiver
android:name=".DbaseChanged"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="Updated" />
</intent-filter>
</receiver>
In App B Broadcast receiver class-
public class DbaseChanged extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent
intent) {
String act = intent.getAction();
if(act != null && act.equals("Updated") )
{
Toast.makeText(context, act ,
Toast.LENGTH_SHORT).show();
}
}
}
I needed to call setPackage("package_name") to ensure explicitness when I registered the broadcast receiver in the Manifest. I was then able to receive the data even if the app was closed completely.
// sending app sends broadcast
Intent intent = new Intent(ACTION_RECOMMEND);
intent.putExtra(LISTEN_RECOMMENDATION, "Triggered - Jhene Aiko");
intent.putExtra(WATCH_RECOMMENDATION, "Goblin - Kim Go-eun");
intent.setPackage("com.example.package.receiverapp");
sendBroadcast(intent);
//receiving app manifest registers receiver
<receiver
android:name=".ManifestRegisteredBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.random.action.RECOMMEND" />
</intent-filter>
</receiver>
I didn't need to add intent.setPackage(package_name) when registering the receiver via an activity, but this also meant that I couldn't get the data if the activity was destroyed (app closed, app in background for long period)
I have registered a BroadcastReceiver in manifest:
<receiver android:name=".OrderReceiver" android:permission="com.google.android.c2dm.permission.SEND" android:exported="true">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.gndp" />
</intent-filter>
</receiver>
and in OrderReceiver class I'm trying to broadcast a received intent this way:
public class OrderReceiver extends BroadcastReceiver {
public void onReceive(Context mContext, Intent intent) {
if(intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
intent.setAction("com.gndp.device.REGISTERED");
mContext.sendBroadcast(intent); //BAZINGA
}
}
This broadcasted intent(BAZINGA) is received in this class(OrderReceiver) but not in another activity where i want to receive it. Here's the activity:
public class RegisterActivity extends Activity {
private BroadcastReceiver deviceRegisteredBroadcastReciever;
#Override
onCreate(){
...
deviceRegisteredBroadcastReciever = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//broadcast never reach here<----PROBLEM
}
}
#Override
onResume() {
...
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(mContext);
lbm.registerReceiver(deviceRegisteredBroadcastReciever, new IntentFilter("com.gndp.device.REGISTERED"));
}
}
Have tried a lot of things including sending broadcast by instance of LocalBroadcastManager, using an inner class instead of BroadcastReceiver in the activity.
RegisterActivity is in foreground when OrderReceiver receives its first broadcast and it stays in foreground.
You send a global broadcast in the OrderReceiver by calling sendBroadcast of Context class and trying to receive this with LocalBroadcastManager.
You should either broadcast with LocalBroadcastManager or register ordinary BroadcastReceiver with Activity.registerReceiver method in the RegisterActivity.
I have a onBootCompleted broadcast receiver registered in the manifest.
It runs starts MyService. My service in the onCreate registers 3 more broadcast receivers dynamically.
The 3 new receivers filter on the following intent actions
LOCALE_CHANGED,
TIMEZONE_CHANGED and
CONNECTIVITY_CHANGED.
These works correctly when I run the application from Eclipse but, after I reboot the device and my service starts up none of receivers work.
I have a work around implementation but, I would like to know why this is happening?
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".receiver.BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="false"
android:stopWithTask="false" >
</service>
Service:
public class MyService()
{
LocationTimeZoneChangedReceiver mLocationTimeZoneChangedReceiver = new LocationTimeZoneChangedReceiver()
NetworkChangedReceiver mNetworkChangedReceiver = new NetworkChangedReceiver()
public void onCreate()
{
registerReceiver(mLocationTimeZoneChangedReceiver, new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
registerReceiver(mLocationTimeZoneChangedReceiver, new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED));
registerReceiver(mNetworkChangedReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
BootCompletedReceiver:
public class BootCompletedReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent){}
}
MyApplication:
public class MyApplication extends Application
{
ServiceConnection mServiceConnection = new ServiceConnection() { anonymous class...}
public void onCreate()
{
bindService(new Intent(this, MyService.class), mServiceConnection,Context.BIND_AUTO_CREATE);
}
}
Edited:
Edited code for Plinio.Santos.
It's a big app with many moving parts so at best I can post small code snippets.
Following are the steps I am following for testing:
Push app via Eclipse,
test that network change receiver is working
leave wifi off
Now restart the device
wait for the process to start and turn on wifi.
I believe that the service is not started or bound due errors. Unfortunately I can not say it for sure without all binding/starting code.
Anyway, you can see bellow a code that worked fine after I rebooted (the app started, registered the receiver and is receiving the CONNECTIVITY_CHANGED broadcast.
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".TestReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".TestService"
android:exported="true" />
Receiver class:
public class TestReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Toast.makeText(context, "Intent.ACTION_BOOT_COMPLETED receiverd !", Toast.LENGTH_SHORT).show();
context.startService(new Intent(context, TestService.class));
}
}
}
Service class:
public class TestService extends Service {
private BroadcastReceiver mConnectivityChangedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
Toast.makeText(context, "ConnectivityManager.CONNECTIVITY_ACTION receiverd !", Toast.LENGTH_SHORT).show();
}
}
};
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
registerReceiver(mConnectivityChangedReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
I'm starting with android and I'm having some lessons to learn some android concepts. In this case, I'm practicing with the BroadCast receivers.
I have to create a BroadCast Receiver that when I boot the phone/emulator, starts an activity which shows a plain text.
I have this class:
public class MainActivity extends Activity {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
IntentFilter mfilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(receiver, mfilter);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
But i'm not getting to do what I need, it simply does nothing, so... What I'm doing wrong here?
In the manifest I just have the activity declared.
Try this...
Step1:
set the permission in AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step2:
Add this is intent filter in receiver,
<receiver android:name=".BootReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Step3:
Now you can start your application's first activity from onReceive method of Receiver class..
public class BootReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
You can not register ACTION_BOOT_COMPLETED receiver dynamically(it's not a way).
ACTION_BOOT_COMPLETED receiver has to be registered statically in manifest file
ex
<receiver android:name="com.myapp.receiver.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
When you register a receiver in Activity or in Service it's lifetime is bound with them
You need to define your receiver inside app manifest.
For ex:
Class
class MyClass extends BroadCastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
IntentFilter mfilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(receiver, mfilter);
}
}
Create your own MyReceiver class by extending BroadcastReceiver class, and register your MyReceiver in the manifest with filter ACTION_BOOT_COMPLETED.
<receiver android:name="com.test.MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I want to set a broadcast receiver to run some function when it gets the broadcast message, in this example, I want to catch the download's manager intent:
DownloadManager.ACTION_DOWNLOAD_COMPLETE
I looked at the Android API examples and haven't found a way to do this
You should read this first:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
Then look here for examples.
Android Samples: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/appwidget/ExampleBroadcastReceiver.html
Blog post: http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-ii-intent-receivers/
Try this:
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ){
// do something
}
}
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
You can create a class that inherits from BroadcastReceiver:
public class MyDownloadCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}
And then register this class in your application manifest like so:
<receiver android:enabled="true"
android:name="MyDownloadCompleteReceiver"
android:label="downloadCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>