I have 3 android apps. If a user has app1 open, is should check to see if app2, and/or app3 are running, and if so, kill them.
Here is what I have tried.
Implemented a BroadcastReceiver() in each app.
mFinishReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.v("intent", "Got Kill Intent, I am " + pn);
if (killAction.equals(intent.getAction())) {
killmePlease(pn, killAction);
}
}
};
registerReceiver(mFinishReceiver, intentFilter);
and then, using this I try to kill the app..
private void killmePlease(String pn) {
Intent _killme = new Intent(this, MainActivity.class);
startActivity(_killme);
try {
Log.v("intent", "Finishing " + pn);
setResult(RESULT_OK);
MainActivity.this.finish();
System.exit(0);
Log.v("intent", "Finished");
return;
} catch (Exception ex) {
Log.v("intent", "Error Killing " + ex.getMessage());
}
}
However, the apps are still running with no errors thrown. Can anyone kindly tell me what I am missing?
Related
I would like to set my own activity infront the call screen. I have seen that there are many examples for this but with the older versions of android, while I want it to work with android 6.0 and above. This means that I have to deal with the permissions. I managed to grant the necessary permissions. After that I make a Class that inherits BroadcastReceiver so that I can detect when the phone is ringing, the only problem is that I can't send my activity infront of the call display. These are some of the classes I use:
public class PhoneStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Toast.makeText(context, " Receiver start ", Toast.LENGTH_SHORT).show();
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing State Number is -", Toast.LENGTH_SHORT).show();
Intent dialogIntent = new Intent(context, LockActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
context.startActivity(dialogIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class LockActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_screen);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
+WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
+WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Button btnLock = (Button) findViewById(R.id.btnUnlock);
final EditText txtPass = (EditText) findViewById(R.id.txtPass);
btnLock.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String pass = txtPass.getText().toString();
if(pass.equals("pass")||pass.equals("пасс")) {
finish();
}else{
Toast.makeText(LockActivity.this, "Wrong password!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
If anything else is needed please ask!
I managed to solve it, the problem is that it takes time to start the in-built call activity, so my activity started first and the other went on top of it. Therefore I made the current thread of my activity to sleep for less than a second. The in-built activity was launched and then my activity went on top of it.
public class PhoneStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Toast.makeText(context, " Receiver start ", Toast.LENGTH_SHORT).show();
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing State Number is -", Toast.LENGTH_SHORT).show();
Intent dialogIntent = new Intent(context, LockActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Thread.sleep(700);
context.startActivity(dialogIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
my application catches calls in background using broadcast receiver and after that the app is launched. But the launch has a serious delay (20-30 seconds on Android 4.1.2). How can I speed up the app launch?
#Override
public void onReceive(Context context, Intent intent) {
phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(!mLastState.equals(phoneState)){
switch (phoneState){
case("RINGING"):
mLastState="RINGING";
try {
Intent i =context.getPackageManager().getLaunchIntentForPackage("com....");
context.startActivity(i);
} catch (Exception e) {
// TODO
}
break;
case("OFFHOOK"):
mLastState="OFFHOOK";
break;
case("IDLE"):
mLastState="IDLE";
break;
default:
break;
}
}
}`
I am able to retrieve incoming call number in other version of android but unable to retrieve incoming call number in marshmallow android
public void onReceive(Context context, Intent intent) {
mContext=context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" + incomingNumber, Toast.LENGTH_LONG).show();
final Thread thread=new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
intentStart = new Intent(mContext.getApplicationContext(), MainActivity.class);
intentStart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intentStart);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
}}
With minor modifications, your code worked fine on my Nexus 6 using MRA58K. I removed line 2 and used context instead of mContext. I also declared a local intent on line 13.
It's happens when application don't have PHONE permission.
How it's works - http://developer.android.com/intl/es/training/permissions/index.html#permission-groups
When I get UserRecoverableAuthIOException in AbstractThreadedSyncAdapter, I'm creating a notification as below.
Here's how I'm creating the service:
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i("Service", "Thread started");
return this.getSyncAdapter().getSyncAdapterBinder();
}
private GoogleTasksSyncAdapter getSyncAdapter() {
if (syncAdapter == null)
{
Log.i("Service", "syncAdapter started");
syncAdapter = new MySyncAdapter(this);
}
return syncAdapter;
}
Once the thread is started, I'm raising a notification. But once user clicks on the notification, they can see the authorization activity. After authorising how to resume from the last point. I.e how to get notified once the activity is closed in Syncadapter.
The SyncAdapter thread are running, and you want to get notification when SyncAdapter ends, right?
So, you can comunicate the SyncAdapter thread with BroadCast.
In your SyncAdapter class:
Intent i = new Intent(SYNC_FINISHED);
context.sendBroadcast(i);
Log.i(TAG, "Network synchronization complete");
In a activity or a fragment:
private BroadcastReceiver syncFinishedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Sync finished!!");
// Here you can send your notification or another thing that you want
}
};
#Override
public void onStart() {
super.onStart();
getActivity().registerReceiver(syncFinishedReceiver, new IntentFilter(SyncAdapter.SYNC_TASK_FINISHED));
}
#Override
public void onStop() {
super.onStop();
getActivity().unregisterReceiver(syncFinishedReceiver);
}
NOTE: The SYNC_FINISHED constant, you can define previously in your SyncAdapter
I hope I've helped you.
Greetings!
In your SyncAdapter you do something like:
#Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.i(TAG, "Beginning network synchronization");
if(extras.getBoolean(RUN_METHOD_1) || extras.getBoolean(RUN_ALL)) {
method1();
}
if(extras.getBoolean(RUN_METHOD_2) || extras.getBoolean(RUN_ALL)) {
method2();
}
}
public void method1(){
try{
// do something
} catch (Exception e) {
e.printStackTrace();
// here you can send your notification when exception occours.
}
}
public void method2(){
try{
// do something
} catch (Exception e) {
e.printStackTrace();
// here you can send your notification when exception occours.
}
}
in your "authorization" code you do something like:
Bundle b = new Bundle();
b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
b.putBoolean(SyncAdapter.RUN_METHOD_1, true);
ContentResolver.requestSync(account, CONTENT_AUTHORITY, b);
so you can run where the sync stopped.
Greetings!!!
Here is the solution,
we need to use the syncResult.stats.numAuthExceptions to tell about exception, it throws message automatically. syncResult.delayUntil will wait and restart sync after elapsing time
How to show sync failed message
I am trying to implement an auto logout functionality. If the application is not used for a period of 1 hour i would want to automatically bring the user to the login screen. When the phone gets locked the thread which is monitoring if the application is in use does not seem to continue. My code is as below:
/**
* This Deamon checks if the application is idle and
*/
private class LogoutDeamon extends Thread {
public void run() {
while (!logoutDeamon) {
try {
System.out.println("Logout Counter:" + logoutConter);
if (logoutConter <= 0) {
logoutDeamon = true;
ApplicationManager.getInstance().setLoggedOut(true);
ApplicationManager.getInstance().Log(Level.INFO, "Auto Log out");
logout();
} else {
decreamentCounter();
}
sleep(60000 * 1);
} catch (ParserException ex) {
Log(Level.ERROR, " Par. Ex. in Logout-Deamon:" + ex.getMessage());
logout();
} catch (ServerException ex) {
Log(Level.ERROR, " Ser. Ex. in Logout-Deamon:" + ex.getErrorMessage());
logout();
} catch (InterruptedException ie) {
Log(Level.ERROR, "Int. Ex. in Logout-Deamon:" + ie.getMessage());
} catch (Exception ex) {
Log(Level.ERROR, "Erro in Logout-Deamon:" + ex.getMessage());
logout();
}
}
}
private void logout(){
Intent broadcastIntent = new Intent();
broadcastIntent
.setAction("com.package.ACTION_LOGOUT");
applicationContext.sendBroadcast(broadcastIntent);
Intent loginIntent = new Intent(applicationContext,
Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
applicationContext.startActivity(loginIntent);
}
}
Am i missing out on something? Can someone kindly help me with this. Thanks in advance.
I don't think that you need a thread. Register the time in onPause. In onResume test if 1 hour has passed. If not, reset the timer.