Using broadcastreceiver only on certain activity - android

I have two activities on is Register() and the other is ReadNews(). I am using broadcast receiver to detect internet connection automatically to execute some code.
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
if(MyApplication.isActivityVisible() == true) {
Log.d("WifiReceiver", "Have Wifi Connection");
Toast.makeText(context, "تم الإتصال بالشبكة", Toast.LENGTH_LONG).show();
if (context instanceof RegisterActivity){
Intent i = new Intent(context,
ReadNews.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
else {
}
}
}
How can I start the ReadNews() activity only when the internet connection goes on while the user still using the RegisterActivity() only?
I tried to use the context like if (context instanceof RegisterActivity) but that doesn't seem right.

As I said in a comment, you don't need an inner class, you can just create a stand-alone class, like this:
public class MyWifiReceiver extends BroadcastReceiver {
private Activity activityToFinish;
MyWifiReceiver(Activity activityToFinish) {
this.activityToFinish = activityToFinish;
}
#Override
public void onReceive(Context context, Intent intent) {
// do what ever you want here
...
// finish the activity
activityToFinish.finish();
}
}
and in RegisterActivity, declare a private member variable like this:
private MyWifiReceiver wifiReceiver;
in RegisterActivity.onCreate() create an instance of the receiver passing the Activity in the constructor, like this:
wifiReceiver = new MyWifiReceiver(this);
then register and unregister the wifiReceiver as you've shown in your own answer.

Thanks to #Squonk comment, I have followed that short comment instructions to accomplish what I want to do and now I want to share summary of my code to show how this thing works:
Broadcast receiver inner class
private BroadcastReceiver wifiReceiver =
new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//do what ever you want here
};
registering Broadcast receiver in onResume()
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); //or any intent filter you want
registerReceiver(wifiReceiver, filter);
}
unregistering Broadcast receiver in onPause
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(wifiReceiver);
}
Important
Don't use an <intent-filter> in the manifest.
registering the broadcast receiver must be dynamic (onResume,onPause)

Related

Can a Broadcast Receiver send information to an Activity only if it is active without starting a new Activity?

I have a broadcast receiver that gets triggered on geofencing events and either "clocks in" or "clocks out" with the server. If my application's "Attendance" activity is already open I would like it to display the clocking status change but I don't want the Broadcast Receiver to start the activity if it's not open - in other words display the change "live" while the activity is open only.
The way I imagine doing this is with the Broadcast Receiver sending an Intent to the activity but name "startActivity()" doesn't sound encouraging unless there are any special flags I can pass to prevent starting an Activity that isn't already open - I can't seem to find any.
The other option would be to constantly poll the value while the activity is open but it doesn't seem optimal so I would only use it if there wasn't another way and I can't think of a reason why it couldn't be possible with Intents.
There are several different ways to accomplish the same task. One is registering a listener like the following example:
MainActivity
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Receiver.setOnReceiveListener(new Receiver.OnReceiveListener() {
public void onReceive(Context Context, Intent intent)
{
//Do something.
}
});
}
#Override
protected void onDestroy()
{
super.onDestroy();
Receiver.setOnReceiveListener(null);
}
}
Receiver
public class Receiver extends BroadcastReceiver
{
private static OnReceiveListener static_listener;
public static abstract interface OnReceiveListener
{
public void onReceive(Context context, Intent intent);
}
public static void setOnReceiveListener(OnReceiveListener listener)
{
static_listener = listener;
}
#Override
public final void onReceive(Context context, Intent intent)
{
if(static_listener != null) {
static_listener.onReceive(context, intent);
}
}
}
Just have your BroadcastReceiver send a broadcast Intent. Your Activity should register a listener from this broadcast Intent and if it gets triggered, it can update the UI.
Here's an example:
Declare a private member variable in your Activity:
private BroadcastReceiver receiver;
In Activity.onCreate(), register the BroadcastReceiver:
IntentFilter filter = new IntentFilter();
filter.addAction("my.package.name.CLOCK_STATUS_CHANGE");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Here you can update the UI ...
}
};
registerReceiver(receiver, filter);
And in onDestroy() you can unregister it (probably not necessary, but cleaner):
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
In your BroadcastReceiver that detects the geofencing event, you should create and send a broadcast Intent:
Intent broadcastIntent = new Intent("my.package.name.CLOCK_STATUS_CHANGE");
sendBroadcast(broadcastIntent);

Dynamic BroadcastReceiver to Check Online Connectivity

I want to dynamically set up a BroadcastReceiver to check whether or not I am online ONLY when my app is running in the foreground. Within my main class, I created an an Intent Filter, a nested BroadcastReceiver Class and finally register/unregister the receiver within the onResume and onPause methods respectively.
My question is, is there an an intent ACTION that I can add to my intent filter that checks for online connectivity?
If not, how can I create this Dynamic Broadcast Receiver to perform only when my app is running?
Here is what my Class looks like....
public class MainActivity extends AppCompatActivity {
private IntentFilter onlineIntentFilter;
private CheckOnlineReceiver checkOnlineReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onlineIntentFilter = new IntentFilter();
checkOnlineReceiver = new CheckOnlineReceiver();
//*****WHAT INTENT ACTION CAN I PASS TO CHECK NETWORK CONNECTIVITY******
onlineIntentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(checkOnlineReceiver, onlineIntentFilter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(checkOnlineReceiver);
}
private class CheckOnlineReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getApplicationContext(),"IN METHOD, ACTION = " + action, Toast.LENGTH_LONG).show();
}
}}
Add connectivity action
onlineIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
and don't forget to add the permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
The broadcast receiver's onReceive() will be called on connectivity change which means on both connected and disconnected. so whenever you receive the braodcast you have check for connectivity as below
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
Here is the standard way to do this.
Create a interface which will have two methods onNetworkConnected() and onNetworkDisconnected()
public interface NetworkConnectivityListener {
public void onNetworkConnected();
public void onNetworkDisconnected();
}
Any class that wants to listen to network changes will implement this interface and override it's two methods.
Create a class which will extend the BroadcastReceiver and this receiver's onReceive() will catch the connectivity changes. In this class create a function to register the listeners
public class NetworkBroadcastReceiver extends BroadcastReceiver {
private NetworkConnectivityListener listener;
public NetworkBroadcastReceiver(NetworkConnectivityListener listener) {
this.listener = listener;
}
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (listener != null) {
if (isConnectedToInternet()) {
listener.onNetworkConnected();
} else {
listener.onNetworkDisconnected();
}
}
}
PS you can easily check if your device is connected to internet or not. Just google.
Now let's say you want your MainActivity to listen to netwrork changes, all you have to do is implement the NetworkConnectivityListener in your main activity call and create an instance of the NetworkBroadcastReceiver passing the context of MainActivity and it will start to get the network updates.
add intentfilter CONNECTIVITY_ACTION
You will need to create a service as you are looking to perform a long-running task in the background.
A service will need to be registered in the manifest, within the <application> tags.
<service android:name=".WifiService"
android:stopWithTask="true"
android:enabled="true"/>
stopWithTask causes the service to be destroyed when all of your activities are destroyed (the default value is true).
You will need to register and unregister a BroadcastReceiver to the CONNECTIVITY_CHANGE action in the service class.
public class WifiService extends Service {
private BroadcastReceiver mReceiver;
#Nullable
#Override
public IBinder onBind(Intent intent) {
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
// Your BroadcastReceiver class
mReceiver = new WifiReceiver();
registerReceiver(mReceiver, filter);
return null;
}
#Override
public boolean onUnbind(Intent intent) {
unregisterReceiver(mReceiver);
return super.onUnbind(intent);
}
}
And then in the BroadcastReceiver you must listen for CONNECTIVITY_CHANGE.
public class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isConnected = networkInfo != null &&
networkInfo.isConnectedOrConnecting();
if (isConnected) {
switch (networkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
// Connected via wifi
break;
case ConnectivityManager.TYPE_ETHERNET:
// Connected via ethernet
break;
case ConnectivityManager.TYPE_MOBILE:
// Connected via mobile data
break;
}
}
}
}
With this setup you will be able to notify components of your application on connectivity change.

How to send Internet service data to main activity?

I have a variable in FetchAddressIntentService class name addressText which holding zip code using reverse geocode. what i want is to send the addressText data to my MainActivity.
You have to register your main activity to the broadcast and then send the broadcast from the service.
In your main activity:
private BroadcastReceiver receiver;
#Override
public void onCreate(Bundle savedInstanceState){
IntentFilter filter = new IntentFilter();
filter.addAction("BROADCAST_ZIP_CODE");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//get data from the intent and display it
String zipCode = intent.getStringExtra("ZIP_CODE");
//do something with the zipCode
}
};
registerReceiver(receiver, filter);
}
#Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
super.onDestroy();
}
In your service:
public class ZipCodeService extends Service {
#Override
public void onCreate() {
String zipCode = "your_zip_code";
Intent intent = new Intent();
intent.setAction("BROADCAST_ZIP_CODE");
intent.putExtra("ZIP_CODE",zipCode);
sendBroadcast(intent);
}
}
You can achieve this in two ways.
Using a Bound Service and registering your Activity as a listener object with your Service by having your Activity implement a listener interface. Docs here.
Sending a Local Broadcast from the Service to your Activity. (See JackD's answer)
Depending on how you've architected your application you may prefer one solution more than the other. The Android docs will help which is best.

How to pass data from BroadcastReceiver to Activity without in onCreate()

I have a serious issue about passing data from BroadcastReceiver to an Activity. Let see my issue carefully. I have a class PhoneStateReceiver extends BroadcastReceiver that used to received an incoming phone.
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
The incoming phone will be sent to an Activity, called ReceiverActivity. The ReceiverActivity receives the incoming phone and sends it to a server via a socket connection. The socket connection is initialized in the onCreate function. I googled and found server way to pass the data from BroadcastReceiver to an Activity. The common way is that send data via putExtra function and call startActivity. However, the way will call the onCreate again and then connect the socket, draw the UI again. Thus, it is not helpful in my case.
In my goal, If the phone receives an incoming call, it will send the incoming call to the ReceiverActivity. The ReceiverActivity receives the message and calls the send function. Which is the best way to do it? Thank you
The common way to pass data from a BroadcastReceiver to a ReceiverActivity that I used as follows
In PhoneStateReceiver class :
Intent intent_phonenum = new Intent(context, ReceiverActivity.class);
intent_phonenum.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent_phonenum.putExtra("phone_num", incomingNumber);
context.startActivity(intent_phonenum);
In ReceiverActivity class :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connect_socket();
Intent intent = getIntent();
phone_num = intent.getStringExtra("phone_num");
send(phone_num);
}
Simple without any third party libs.
Make sure BroadcastReceiver must be registered and also unregistered on OnPause().
You have to do two thing
Register a receiver in you activity like below.
public class MainActivity extends Activity {
Context context;
BroadcastReceiver updateUIReciver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
IntentFilter filter = new IntentFilter();
filter.addAction("service.to.activity.transfer");
updateUIReciver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//UI update here
if (intent != null)
Toast.makeText(context, intent.getStringExtra("number").toString(), Toast.LENGTH_LONG).show();
}
};
registerReceiver(updateUIReciver, filter);
}
}
Now in you service
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Intent local = new Intent();
local.setAction("service.to.activity.transfer");
local.putExtra("number", incomingNumber);
context.sendBroadcast(local);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
There is a very simple design pattern you can use here to ease communication between your classes and also decouple your code: publisher/subscriber. My favorite library for this is EventBus:
First, add to your build.gradle file:
compile 'org.greenrobot:eventbus:3.0.0'
Then, create a simple POJO - Plain Old Java Object like this:
public class OnReceiverEvent{
private String phoneNumber;
public OnReceiverEvent(String phone){
this.phoneNumber = phone;
}
public String getPhoneNumber(){
return phoneNumber;
}
}
Next, by making your Receiver class a publisher, and your Activity a subscriber, you should be able to easily pass the information to your activity like this:
//inside your PhoneStateReceiver class when you want to pass info
EventBus.getDefault().post(new OnReceiverEvent(phoneNumber));
Next, inside your activity, simply do this:
//onStart
#Override
public void onStart(){
super.onStart();
EventBus.getDefault().register(this);
}
//onStop
#Override
public void onStop(){
super.onStop();
EventBus.getDefault().unregister(this);
}
Finally, handle the posted data i.e phoneNumber value:
#Subscribe
public void onPhoneNumberReceived(OnReceiverEvent event){
//get the phone number value here and do something with it
String phoneNumber = event.getPhoneNumber();
//display or something?
}
UPDATE
If you have another Event that you want this activity to subscribe to, simply create a method like you did in the first one using the #Subscribe annotation.
#Subscribe
public void onSomeOtherEvent(EventClassName event){
//get the variables here as usual;
}
This is the easiest way to pass the data from your receiver to your activity without having to worry about starting the activity over and over!
I hope this helps and good luck!
So I understand you don't want to recreate your Activity everytime.
In your Intent changing this flag will help you :
intent_phonenum.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
In Intent class if you read method summary of FLAG_ACTIVITY_CLEAR_TOP:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that activity,
all of the other activities on top of it will be closed and this Intent
will be delivered to the (now on top) old activity as a new Intent. (you can read more ...)
In this case: If your app is running and you have an Activity instance then Intent will not recreate your Activity. But assume that your app is in closed state and when BroadcastReceiver triggered, the Intent will create new Activity because you don't have instance of that Activity.
#Edit :
You can specify special Intent like that in your BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
Intent i = new Intent("mycustombroadcast");
i.putExtra("phone_num", incomingNumber);
context.sendBroadcast(i);
}
Then in your Activity inside onCreate() register receiver like that :
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
int incoming_number= bundle.getInt("phone_num");
Log.e("incoming number", "" + incoming_number);
}
};
//then register receiver like that :
registerReceiver(broadcastReceiver, new IntentFilter("mycustombroadcast"));
You can unregister Receiver in onDestroy() : unregisterReceiver(broadcastReceiver);
Also another way is overriding onNewIntent() in your Activity:
#Override
protected void onNewIntent(Intent intent) {
//your intent is here, you can do sth.
super.onNewIntent(intent);
}

How to update the UI of Activity from BroadCastReceiver

I am learning Android concepts Activity and BroadCastReceiver. I want to update the content of Activity from the BroadtCastReceiver both are in different java class.
It is something like
MyActivity.java and MyBroadtCastReceiver.java
Is this possible to do this in Android ?
A BroadcastReceiver can be used in many ways but when it comes to something as specific as updating the UI components of an Activity, there is little advantage to declaring / defining a BroadcastReceiver in it's own Java class file.
Reasoning - the BroadcastReceiver has to have some prior "knowledge" of the Activity and what it is required to do in order to update the UI. In effect the BroadcastReceiver is tied to the Activity itself and it makes sense to declare / define it as an inner class.
Another important aspect is the Activity needs to be in a "running" (i.e., visible) state in order to guarantee manipulation of UI components. In this case, registering the receiver in onResume() and unregistering in onPause() will help prevent problems.
Using a generic template I'd do something like the following...
class MyActivity extends Activity {
boolean mIsReceiverRegistered = false;
MyBroadcastReceiver mReceiver = null;
// onCreate(...) here
#Override
protected void onResume() {
// Other onResume() code here
if (!mIsReceiverRegistered) {
if (mReceiver == null)
mReceiver = new MyBroadcastReceiver();
registerReceiver(mReceiver, new IntentFilter("YourIntentAction"));
mIsReceiverRegistered = true;
}
}
#Override
protected void onPause() {
if (mIsReceiverRegistered) {
unregisterReceiver(mReceiver);
mReceiver = null;
mIsReceiverRegistered = false;
}
// Other onPause() code here
}
private void updateUI(Intent intent) {
// Do what you need to do
}
private class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
}
}
EDIT: A couple of extra notes...
The life-cycle of a BroadcastReceiver is between entering and leaving onReceive(...). Once it has returned from onReceive(...) the instance remains in a dormant state waiting for the next broadcast.
Directly related to point 1 - a BroadcastReceiver isn't designed for "heavy lifting". Basically the onReceive(...) method should be kept as simple as possible. Any methods it calls should also be as light-weight as possible...get in, do your stuff, get out then wait for the next broadcast. If updating the UI is going to take some time (perhaps updating a ListView by re-querying a database for a large amount of data for example), consider calling code which performs asynchronously (an AsyncTask for example).
Yes its possible. This is what i do.
Class i send the broadcast from (BackgroundActivity.java):
public static final String BROADCAST_BUFFER_SEND_CODE = "com.example.SEND_CODE";
onCreate(){
bufferIntentSendCode = new Intent(BROADCAST_BUFFER_SEND_CODE);
}
private void sendBufferingBroadcastSendCode() {
bufferIntentSendCode.putExtra("buffering", "1");
sendBroadcast(bufferIntentSendCode);
}
The class it will receive the broadcast(SendCode.java):
onResume(){
registerReceiver(broadcastBufferReceiver, new IntentFilter(BackgroundActivity.BROADCAST_BUFFER_SEND_CODE));
}
// set up broadcast receiver
private BroadcastReceiver broadcastBufferReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent bufferIntent) {
SendCode.this.LoadMessages(alarmNumber);
}
};
I unregister it in onPause
this.unregisterReceiver(broadcastBufferReceiver);
Register a new BroadcastReceiver object in your activity with same intent-filters as your MyBroadtCastReceiver. Since BroadcastReceiver and MyBroadtCastReceiver has same intent-filters both of their onReceive() will be invoked. Whatever update that you want to do in Activity can be done in onReceive of your BroadcastReceiver.
You can do like this:
public class MyActivity extends Activity{
// used to listen for intents which are sent after a task was
// successfully processed
private BroadcastReceiver mUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
new UpdateUiTask().execute();
}
};
#Override
public void onResume() {
registerReceiver(mUpdateReceiver, new IntentFilter(
YOUR_INTENT_ACTION));
super.onResume();
}
#Override
public void onPause() {
unregisterReceiver(mUpdateReceiver);
super.onPause();
}
// used to update the UI
private class UpdateUiTask extends AsyncTask<Void, Void, String> {
#Override
protected void onPreExecute() {
}
#Override
protected String doInBackground(Void... voids) {
Context context = getApplicationContext();
String result = "test";
// Put the data obtained after background task.
return result;
}
#Override
protected void onPostExecute(String result) {
// TODO: UI update
}
}
}
Squonk-s answer only works, if the Activity is active currently.
If you dont want to declare / define your BroadcastReceiver (BR) in an other Activity, or if you want to make some changes even if the app is not foreground, than your solution would look something like this.
First, you declare the BR, and save, or override the data needed to show in Acitvity.
public class MyBR extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// override the data. Eg: save to SharedPref
}
}
Then in Activity, you show the data
TextView tv = findViewById(R.id.tv);
tv.setText(/*get the data Eg: from SharedPref*/);
And you should use a Timer to refresh the tv as well:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView tv = findViewById(R.id.tv);
tv.setText(/*get the data Eg: from SharedPref*/);
}
});
}
}, REFRESH_RATE, REFRESH_RATE);
REFRESH_RATE could be something like 1 second, but you decide.
try like this it may help you.
Define this method in activity's oncreate method in which you want to update ui,
BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//your code to update ui
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("giveyourappname"));
Define this action at place from where you want to update ui,
try{
ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
Log.d("Activity", "Current Activity ::" + taskInfo.get(0).topActivity.getClassName());
Log.d("Package", "Package Name : "+ componentInfo.getPackageName());
if(componentInfo.getPackageName().equals("your application package name")){
Intent intent = new Intent("giveyourappname");
//add data you wnat to pass in intent
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}catch(Throwable e){
e.printStackTrace();
}

Categories

Resources