Close skype app and open my app dynamically after finish "skype calling" - android

I can open skype activity but I dont know how to open my application after "skype calling" finish.
public static void skype(String number, Context ctx) {
try {
Intent sky = new Intent("android.intent.action.VIEW");
sky.setData(Uri.parse("skype:" + number));
ctx.startActivity(sky);
} catch (ActivityNotFoundException e) {
Log.e("SKYPE CALL", "Skype failed", e);
}
}
Close to skype after calling is enough for me first step. I searched phone broadcastrecevier but It doesnt detect anyting.

Related

android - Broadcast of Intent Has extras error

I have a broadcast receiver that detects when a USB device is attached/detached. The app opens when the device is connected however when i disconnect/connect the device to my android multiple times, i get the following ANR error: Does anyone know what is causing this?
ANR error
Here is my Broadcast Receiver code:
String USB_TAG = "USB_TAG";
String BROADCAST_TAG = "BROADCAST_TAG";
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(BROADCAST_TAG, "BroadcastReceiver Event");
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
Log.d(BROADCAST_TAG, "BroadcastReceiver DEVICE ATTACHED");
Toast.makeText(context, "Device Detected", Toast.LENGTH_SHORT).show();
new Thread(() -> {
try {
if(MainActivity.eMRO_Backend != null) {
if (MainActivity.eMRO_Backend.threadsClosed) {
MainActivity.eMRO_Backend = new eMRO_Backend(context);
}
}
} catch (Exception e){
Log.d(BROADCAST_TAG, "BroadcastReceiver attach ex: " + e.getMessage());
}
}).start();
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
Toast.makeText(context, "Device Not Detected", Toast.LENGTH_SHORT).show();
new Thread(() -> {
Looper.prepare();
try {
MainActivity.powerStatusQueue.put(false);
MainActivity.laserKeyStatusQueue.put(false);
if(MainActivity.eMRO_Backend != null)
MainActivity.eMRO_Backend.shutDownThreads();
} catch (Exception e) {
Log.d(BROADCAST_TAG, "BroadcastReceiver detach ex: " + e.getMessage());
}
}).start();
}
}
}
It looks like your broadcast receiver is not the problem. The problem is that you've got code running on your main (UI) thread that is blocking the main (UI) thread. When Android tries to call your broadcast receiver's onReceive(), the call is blocked by something else and that is what is causing the ANR. What you are seeing is a symptom and not the actual cause of the problem.

How make Android not restart app when application stops

I'm trying to make app close when some uncaughtException happens, but android show a message to try to restart the application and the application stays in slow motion or just black screen, I made a class to extends in my activity to overriding the method uncaughtException() to catch logs to send than to sentry.io and using:
public class MyExceptionHandler implements
Thread.UncaughtExceptionHandler {
public MyExceptionHandler(Activity context) {
Thread.setDefaultUncaughtExceptionHandler(this);
}
#Override
public void uncaughtException(final Thread thread, final Throwable ex) {
Log.e("ERROR", ex.toString());
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
You can use
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Got idea from this answer.

Launch another application in background android

In my application, i start another application (not activity) with this code :
protected void launchApp(String packageName) {
Intent mIntent = getPackageManager().getLaunchIntentForPackage(
packageName);
if (mIntent != null) {
try {
startActivity(mIntent);
} catch (ActivityNotFoundException err) {
Toast t = Toast.makeText(getApplicationContext(),
"App not found", Toast.LENGTH_SHORT);
t.show();
}
}
}
but i would like this application (launched by packageName) run in background and not disturb the UI.
Is it possible ?
Thanks!
you can use broadcast receiver in target app and start it with broadcast
Hope it will help
you can use this
startActivity(getPackageManager().getLaunchIntentForPackage("com.example.appName"));
further information please see this link
Android - How to start third party app with package name?

How to stop or pause Pandora and Spotify

I have an app that has a feature to launch an app, Pandora station, or shortcut. That all works fine. Later I want to stop the app I started. This works for most things except Pandora and Spotify don't always close. Sometimes they do but not always. It seems to be related to the current UI state. For instance, it works fine when I have Pandora showing or the home screen showing. When Home Dock or Car Mode is active it does not work. You can see all my source code here: http://code.google.com/p/a2dpvolume/
service.java is the file that has this functionality.
Here is the part of that code that tries to stop the music from playing and then stop the app.
if (bt2.hasIntent()) {
// if music is playing, pause it
if (am2.isMusicActive()) {
// first pause the music so it removes the notify icon
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
sendBroadcast(i);
// for more stubborn players, try this too...
Intent downIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent2 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP);
downIntent2.putExtra(Intent.EXTRA_KEY_EVENT, downEvent2);
sendOrderedBroadcast(downIntent2, null);
}
// if we opened a package for this device, try to close it now
if (bt2.getPname().length() > 3 && bt2.isAppkill()) {
// also open the home screen to make music app revert to
// background
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
// now we can kill the app is asked to
final String kpackage = bt2.getPname();
CountDownTimer killTimer = new CountDownTimer(6000, 3000) {
#Override
public void onFinish() {
try {
stopApp(kpackage);
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, "Error " + e.getMessage());
}
}
#Override
public void onTick(long arg0) {
if (am2.isMusicActive()) {
// for more stubborn players, try this too...
Intent downIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent2 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP);
downIntent2.putExtra(Intent.EXTRA_KEY_EVENT, downEvent2);
sendOrderedBroadcast(downIntent2, null);
}
try {
stopApp(kpackage);
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, "Error " + e.getMessage());
}
}
};
killTimer.start();
}
}
Here is the function stopApp().
protected void stopApp(String packageName) {
Intent mIntent = getPackageManager().getLaunchIntentForPackage(
packageName);
if (mIntent != null) {
try {
ActivityManager act1 = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
// act1.restartPackage(packageName);
act1.killBackgroundProcesses(packageName);
List<ActivityManager.RunningAppProcessInfo> processes;
processes = act1.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo info : processes) {
for (int i = 0; i < info.pkgList.length; i++) {
if (info.pkgList[i].contains(packageName)) {
android.os.Process.killProcess(info.pid);
}
}
}
} catch (ActivityNotFoundException err) {
err.printStackTrace();
Toast t = Toast.makeText(getApplicationContext(),
R.string.app_not_found, Toast.LENGTH_SHORT);
if (notify)
t.show();
}
}
}
Has someone else run into this problem? How can I reliably stop the launched app? I need to first get it to pause and put it in the background. That is the problem I am having. It works for most situations but not all. Some cases Pandora and Spotify will not respond to the key event being sent and they just keep playing. This keeps the notify icon active and makes the app a foreground activity so I can't stop it.
I finally figured out that Pandora does pause music when it sees a headset disconnect. So, I just had to send that disconnect intent so Pandora would pause. Once paused, it was able to be pushed to background and killed.
//Try telling the system the headset just disconnected to stop other players
Intent j = new Intent("android.intent.action.HEADSET_PLUG");
j.putExtra("state", 0);
sendBroadcast(j);
For anyone else trying this; The android.intent.action.HEADSET_PLUG intent is no longer allowed to be broadcast unless you are running as the system.
As the "HEADSET_PLUG" intent is now only supported if called by a system, I found app specific intents to be the way to go:
Intent pauseSpotify = new Intent("com.spotify.mobile.android.ui.widget.PLAY");
pauseSpotify.setPackage("com.spotify.music");
sendBroadcast(pauseSpotify);
Essentially, what this does, is it calls "PLAY" from the spotify app.
I got the idea from an article and applied it to normal android.

How to find out when an installation is completed

I am creating an application that installs apps downloaded from a server. I would like to Install these application After the file is downloaded the code for the method I am using to install is here:
public void Install(String name)
{
//prompts user to accept any installation of the apk with provided name
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File
(Environment.getExternalStorageDirectory() + "/ContentManager/" + name)), "application/vnd.android.package-archive");
startActivity(intent);
//this code should execute after the install finishes
File file = new File(Environment.getExternalStorageDirectory() + "/ContentManager/"+name);
file.delete();
}
I would like to have the apk file deleted from the sd card after the install is completed. This code deletes it once the install is started, causing the installation to fail. I am fairly neew to android and would much appreciate some help. I am basically trying to wait for the installation to complete before continuing with the process.
The Android package manager sends various broadcast intents while installing (or updating / removing) applications.
You can register broadcast receivers, so you will get notifications e.g. when a new application has been installed.
Intents that might be interesting for you are:
ACTION_PACKAGE_INSTALL
ACTION_PACKAGE_REPLACED
ACTION_PACKAGE_CHANGED
ACTION_PACKAGE_ADDED
Using broadcast receivers is not a big deal:
BroadcastReceiver myReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do whatever you want to do
}
};
registerReceiver(myReceiver, new IntentFilter("ACTION"));
unregisterReceiver(myReceiver);
This might not be the best way but I solved the problem. Here is my new code for the method.
public void Install(final String name,View view)
{
//prompts user to accept any installation of the apk with provided name
printstatus("Installing apk please accept permissions");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File
(Environment.getExternalStorageDirectory() + "/ContentManager/" + name)), "application/vnd.android.package-archive");
startActivity(intent);
try {
Thread.sleep(1500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(int i=0;i<100;)
{
System.gc();
if(view.getWindowVisibility()==0)
{
i=200;
System.gc();
}
try {
Thread.sleep(500);
System.gc();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
File file = new File(Environment.getExternalStorageDirectory() + "/ContentManager/"+name);
file.delete();
}
I created a loop that will wait until the window is in the front to let the method continue executing. The garbage collector and thread sleeping prevents it from slowing down the system or the Linux kernel killing the process. The sleep before the loop is needed so the package manager has time to start before the loop begins.

Categories

Resources