Start any android application from broadcast receiver without delay - android

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;
}
}
}`

Related

Set Custom call activity

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();
}
}
}

Unable to retrieve number before incoming call in marshmallow

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

Killing Android app 2 from app 1

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?

How can I minimize a phone call programmatically?

I have an App where a "Help Call" can be invoked. And I want that the call is running in background when sombody had accept the call. So is there a way to minimize the phone call and meanwhile get back to the app?
You can try something like this
private class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
try {
//THIS WILL SIMULATE A HOME BUTTON PRESS
//Effectively Minimizing the In Call Screen
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(startMain);
//Now that you are home, and your In Call Screen is minimized
//move back to your application
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
}

How to block Specific outgoing calls

I want to block a specific phone number that is in my database
I do a comparison between the number the user dialed, and the number in memory. If they are equal, I block the call.
My code:
public void onReceive(Context context, Intent intent) {
PlaceDataSQL placeDataSQL =new PlaceDataSQL(context);
ArrayList<String> getUsersPhoneNumbers= placeDataSQL.getUsersPhoneNumbers();
//TODO
//===========
//here I need to check the number
Bundle b = intent.getExtras();
String incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
//String outGoingNumber = b.getString(TelephonyManager.);
Boolean find=false;
try {
for(int i=0;i<getUsersPhoneNumbers.size();i++)
{
if(incommingNumber.equals(getUsersPhoneNumbers.get(i)))
{
find=true;
break;
}
}
} catch (Exception e) {
incommingNumber="";
}
// ========================================
//here the problem
//=========================================
String phonenumber=b.getString(Intent.EXTRA_PHONE_NUMBER);
try {
for(int i=0;i<getUsersPhoneNumbers.size();i++)
{
if(phonenumber.equals(getUsersPhoneNumbers.get(i)))
{
find=true;
break;
}
}
if (!find)
return;
}catch (Exception e) {
phonenumber="";
}
if (!find)
return;
/* examine the state of the phone that caused this receiver to fire off */
String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
logMe("Phone Ringing: the phone is ringing, scheduling creation call answer screen activity");
Intent i = new Intent(context, CallAnswerIntentService.class);
i.putExtra("delay", 100L);
i.putExtra("number", incommingNumber);
context.startService(i);
logMe("Phone Ringing: started, time to go back to listening");
}
if (phone_state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Intent i = new Intent(context,InCallScreenGuardService.class);
i.putExtra("delay", 100L);
i.putExtra("number", phonenumber);
logMe("Phone Offhook: starting screen guard service");
context.startService(i);
}
if (phone_state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Intent i = new Intent(context,InCallScreenGuardService.class);
logMe("Phone Idle: stopping screen guard service");
context.stopService(i);
}
return;
}
The problem:
I can get incoming numbers but I can't get outgoing numbers?
You will need a BroadcastReciever for this.
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}

Categories

Resources