Bring the another App in the front when receiving the broadcast - android

I am sending a broadcast from App A to App B and I am already receiving it in app B. After receiving the broadcast I want to bring the App B to the front. How can I bring the App B to the front after receiving the broadcast from the App A?
package com.mysender;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent=new Intent();
intent.setAction("com.mysender.Data");
intent.putExtra("path", "/Download/income_tax_return.pdf");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
}
}
In the App B I am doing the following in the MainActivity:
package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Receive broad Cast fromn External App
IntentFilter filter = new IntentFilter("com.mysender.Data");
registerReceiver(myReceiver, filter);
}
private MyReceiver myReceiver =new MyReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String path = intent.getStringExtra("path");
Toast.makeText(context,"Data Received from External App: " + path, Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
}

Related

close the started activity in my case

I have a class which doesn't extend Activity or Fragment. It is a independent class. I would like to use that class to control an Activity start and finish.
public class MyActivityManager() {
public MyActivityManager(Context context) {
mContext = context;
}
public void startMainActivity() {
Intent intent = new Intent(mContext, MainActivity.class);
mContext.startActivity(intent);
}
public void closeMainActivity() {
// how can I close the started main activity from the other function here?
}
}
As you can see, I started MainActivity from one function, and in another function, I would like to close the MainActivity started. But how can I have a reference to the started MainActivity?
(My main purpose is to let upper caller to use this MyActivityManager to start and close MainActivity)
If the current way is not possible, how to achieve what I want?
//Try the below code and let me know if any issues.
package com.example.raghavendrapai.myapplication;
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 MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mCloseReceiver, new IntentFilter("close_main_activity"));
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mCloseReceiver);
}
private BroadcastReceiver mCloseReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("close_main_activity")) {
finish();
}
}
};
}
// And in your class
package com.example.raghavendrapai.myapplication;
import android.content.Context;
import android.content.Intent;
/**
* Created by raghavendra.pai on 08/03/18.
*/
public class MyActivityManager {
private Context mContext;
public MyActivityManager(Context context) {
mContext = context;
}
public void startMainActivity() {
Intent intent = new Intent(mContext, MainActivity.class);
mContext.startActivity(intent);
}
public void closeMainActivity() {
Intent intent = new Intent("close_main_activity");
mContext.sendBroadcast(intent);
}
}

Invoke service when screen turns on (Android)

I am creating a lockscreen app with facial recognition. As a first step I am creating a simple lockscreen that unlocks on Button click. The lockscreen should start when Switch on MainActivity is turned on. I created a Service which starts my lockscreen activity, but the activity does not show again once I turn off and then turn on my screen.
I am a beginner in android and don't understand what to do. I would be happy if i could get some help.
The java files are:
MainActivity.java
package com.sanket.droidlockersimple;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
public class MainActivity extends ActionBarActivity {
private Switch enableLocker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enableLocker = (Switch) findViewById(R.id.enableLocker);
enableLocker.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(enableLocker.isChecked()) {
Intent intent = new Intent(MainActivity.this, DroidLockerService.class);
startService(intent);
}
}
});
}
}
DroidLockerService.java
package com.sanket.droidlockersimple;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class DroidLockerService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Intent intent1 = new Intent(this, LockScreen.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
LockScreen.java
package com.sanket.droidlockersimple;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class LockScreen extends ActionBarActivity {
private Button unlock;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_lock_screen);
Button unlock = (Button) findViewById(R.id.button);
unlock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
I believe that what you need is a BroadcastReceiver
You have a service running, but something needs to tell the service that the screen has turned on or off. Android accomplishes this by issuing broadcast events whenever certain things occur (i.e. phone call received, system turned on, etc). Any registered broadcast receiver that has included the relevant BroadcastIntent will be informed that the event has occurred.
Your Broadcast Receiver can then tell your service that the event has occured and your service can respond appropriately.
I think that the particular broadcast events that you need to register for are "ACTION_SCREEN_ON" and "ACTION_SCREEN_OFF". See the Intent developer reference page for details.
Also read the developer guide for intents and intent filters in Android, and here is the BroadcastReceiver reference page. (StackOverflow won't let me post more than 2 links yet...just search for it on the Android developer website.)

Update widget when preferences changed using custom broadcast

I need to update my app's widget every time the settings are changed by the user.
I have a settings activity:
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class SettingsActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String customIntent = "CUSTOM_SETTINGS_CHANGED";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new MainActivity.SettingsFragment())
.commit();
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Intent intent = new Intent();
intent.setAction(customIntent);
this.sendBroadcast(intent);
}
}
I've added that custom intent to the manifest:
<action android:name="CUSTOM_SETTINGS_CHANGED" />
But the widget is not updated immediately after the settings are changed. So my custom broadcast either is not sent or not received. What's wrong with my code?
// this part important in manifest declaration
package com.xmpls.onetwothree.abc;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class SettingsActivity {
public class SettingsActivity extends Activity implements
SharedPreferences.OnSharedPreferenceChangeListener {
// Change like this
public static final String CUSTOM_SETTINGS_CHANGED = "com.xmpls.onetwothree.abc.custompls";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction();
/* More code here. . .*/
}
/* And some here. . .*/
}
================================================================================
and in manifest U must registred ACTION like this:
<action android:name="com.xmpls.onetwothree.abc.CUSTOM_SETTINGS_CHANGED" />
I've used a different approach.
In my main activity I've added this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<...>
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Intent intent = new Intent();
intent.setAction(customIntent);
sendBroadcast(intent);
}
};
<...>
}
It seems that onSharedPreferenceChanged was never called for some reason. I don't know why but at least I've found a way to make this work.

Communication between BroadcastReceiver and Activity - android

I have a broadcast receiver in my app which is fired every time the user gets an incoming call. Now, when it happens, I need the broadcast receiver to invoke a specific method in a specific activity. Now, I tried to make this method static and therefore available, but something tells me it is a very bad idea.
Accordingly, I tried to instantiate the broadcast receiver inside my activity without declaring it in my manifest but the problem is - when the app is off, the activity dosn't exist and therefore I can't invoke my method.
So my question is - How can I invoke this method when the broadcast receiver is fired up, without making it "public static"?
Here is my activity code(I have deleted the irrelevant parts)
package com.silverfix.ringo.activities;
import com.silverfix.ringo.R;
import com.silverfix.ringo.activities.fragments.DataManagerFragment;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class RingtonesActivity extends Activity{
private DataManagerFragment dataManagerFragment;
private IntentFilter filter;
private BroadcastReceiver phoneCall;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ringtones);
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayHomeAsUpEnabled(true);
dataManagerFragment = new DataManagerFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(dataManagerFragment, "DataManagerFragment");
ft.commit();
filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
phoneCall = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
dataManagerFragment.act();
}
};
registerReceiver(phoneCall, filter);
}
}
You can use observers , like
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
ObservableObject.getInstance().updateValue(intent);
}
}
public class MainActivity extends Activity implements Observer {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ObservableObject.getInstance().addObserver(this);
}
#Override
public void update(Observable observable, Object data) {
Toast.makeText(this, String.valueOf("activity observer " + data), Toast.LENGTH_SHORT).show();
}
}
public class ObservableObject extends Observable {
private static ObservableObject instance = new ObservableObject();
public static ObservableObject getInstance() {
return instance;
}
private ObservableObject() {
}
public void updateValue(Object data) {
synchronized (this) {
setChanged();
notifyObservers(data);
}
}
}
Receiver can be used via manifest.
ObservableObject - must be singleton.
This might help: how can I notify a running activity from a broadcast receiver?
Also, you can try using Observers
Something like:
public class BroadcastObserver extends Observable {
private void triggerObservers() {
setChanged();
notifyObservers();
}
public void change() {
triggerObservers();
}
}
In your broadcast receiver:
#Override
public void onReceive(Context context, Intent intent) {
BroadcastObserver bco = new BroadcastObserver();
bco.change();
}
and the Activity:
public class YourActivity extends Activity implements
Observer {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BroadcastObserver bco = new BroadcastObserver();
bco.addObserver(this);
}
#Override
public void update() {
//TODO: call your desired function
}
}
If anyone needs two-way communication between a BroadcastReceiver and a Activity, I wrote this utility class which simplifies invoking Methods on each other while still being memory-safe.
https://gist.github.com/Jenjen1324/4a0c03beff827082cb641fc8fe2c4e71

onReceive never called

I have the following code
I want to detect the screen off of the phone
But the function onReceive never is never called
can anyone give me a solution.
Please help.
if anyone has anyother working code please help.
Thank you
package com.pack;
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 Screen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
Context con = this;
this.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Screen off...................");
}
}, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
}
Your code works for me. I modified the code so it's easier to read and added some sysouts, but it works the way you wrote it:
public class Test extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Screen off...................");
}
}, filter);
}
#Override
protected void onResume() {
super.onResume();
System.out.println("onResume()");
}
#Override
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy()");
}
#Override
protected void onPause() {
super.onPause();
System.out.println("onPause()");
}
It prints the following if I do (start activity, turn screen off, turn screen on, press back button):
onResume()
onPause()
Screen off...................
onResume()
onPause()
onDestroy()
After this the activity is destroyed and therefore no events are displayed. Maybe you want to register your BroadcastReceiver in the AndroidManifest instead.

Categories

Resources