Android: Redirect outgoing calls - java

I'm trying to redirect outgoing calls to a different phone number on an Android device. So, I use a BroadcastReceiver "listening" for the NEW_OUTGOING_CALL intent, on his onReceive() method I use the setResultData() method to change the phone number.
Like this:
public void onReceive(Context arg0, Intent arg1) {
setResultData("351978923221");
}
In the emulator all goes well, but on my real device (a crappy ZTE X850 with Android 2.1 I believe) it doesn't if the calling Intent originates in an Activity which is part of the same application. After the dialing screen appears, the phone terminates the call.
Any thoughts out there on why this happens?
Note: I know my question is basically the same as this one but I chose to ask it again anyway to provide additional details about what goes wrong.
Manifest File
An excerpt...
<receiver android:name=".OutgoingCallDetection" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"
android:priority="9999" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

I cut the dialed call and redialed the new call. It worked perfectly on the device.
This is the code part:
setResultData(null);
Uri uri = Uri.fromParts("tel", "!Number to be dialed!", null);
Intent newIntent = new Intent(Intent.ACTION_CALL, uri);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
Hope this helps.

Related

Android Broadcasts - sending and receiving with permissions

I try to implement the permission approach of BroadcastReceiver.
In sender class, I try to send a normal broadcast
Intent intent = new Intent();
intent.setAction("com.vn.BroadcastReceiver");
intent.putExtra("Foo", "Bar");
sendBroadcast(intent);
with AndroidManifest file
<uses-permission android:name="com.nhp"/>
In receiver class, I write a custom FirstBroadcastReceiver
public class FirstBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = FirstBroadcastReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: " + intent.getStringExtra("Foo"));
}
}
and its AndroidManifest
<permission android:name="com.nhp" />
<receiver
android:permission="com.nhp"
android:exported="true"
android:name=".receivers.FirstBroadcastReceiver">
<intent-filter>
<action android:name="com.vn.BroadcastReceiver" />
</intent-filter>
</receiver>
I install receiver application first and sender then. And it worked!!
But when I try to call sendBroadcast(intent,"com.nhp");, it never trigger any thing. Anyone can explain for me why? Thank you
The custom permission should be declared in the sender and the receiver should request for it.
Moreover, broadcast receiver doesn't need the android:permission attribute. You should only use it if sender must have a specific permission.
Sender
The sender declares the custom permission in its manifest
<permission android:name="com.nhp" />
and sends the broadcast
Intent intent = new Intent();
intent.setAction("com.vn.BroadcastReceiver");
intent.putExtra("Foo", "Bar");
sendBroadcast(intent, "com.nhp");
Receiver
The receiver requests for the custom permission in its manifest and listens for broadcasts
<uses-permission android:name="com.nhp"/>
...
...
...
<receiver
android:exported="true"
android:name=".receivers.FirstBroadcastReceiver">
<intent-filter>
<action android:name="com.vn.BroadcastReceiver" />
</intent-filter>
</receiver>
Of course, the order of the installations is important and the sender app must be installed first as it defines the custom permission.
Note: Custom permissions are registered when the app is installed. The app that defines the custom permission must be installed before the app that uses it.

Start service at boot time and launch activity from onReceive method of BR

First I wan't to say that I tried to find my answers on stackoverflow with no success. Second, I tested my code on my Samsung S3 4G 4.1.2.
I have some question with the code I produced.
---Exlaination :
The code starts ClsAutoStart at BOOT_COMPLETED, which starts a service, SvcFirst, which register a BroadcastReceiver synchronized with NEW_OUTGOING_CALL action. In the onReceive() method, I start the activity.
You understood that the activity is supposed to popup when the is a call (no condition yet).
---Questions :
This works but I DO NOT have the exact expected behaviour so I have some questions :
1- Everything goes well ONLY if I first open the activity AFTER installation and BEFORE reboot. Then, after that, every piece of code does what it has to. Could anyone explain me why I have to launch the activity in order it to work ? How to avoid it ?
2- The reason why I use this method to do things is because I don't want my application to appear in the application list (for the user). I wan't the activity to be launched by the onReceive method.
I tried to remove the following lines from the manifest file but I go back to question "1" : if I can't previously launch the activity, it doesn't work (no possibility to launch anything because the lines are removed).
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Could anyone explain me why I doesn't work and how to correct it ?
Is this the correct way to do "hide" my application ?
On stackoverflow, I found many answers ... this is one of them (question "3" is one of the others ...).
3- I removed the previous lines (question "2) and add the following one in the receiver section (of BOOT_COMPLETED action) in my manifest file but it still doesn't change anything. Any clue ?
<category android:name="android.intent.category.HOME" />
---Code :
manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.frontal.invisible04"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.frontal.invisible04.ActMain"
android:label="#string/title_activity_act_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.frontal.invisible04.SvcFirst">
</service>
<receiver
android:name="com.frontal.invisible04.ClsAutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!--<category android:name="android.intent.category.HOME" />-->
</intent-filter>
</receiver>
</application>
</manifest>
ClsAutoStart :
public class ClsAutoStart extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
{
context.startService(new Intent(context, SvcFirst.class));
}
}
}
SvcFirst :
public class SvcFirst extends Service
{
Context context;
private BroadcastReceiver br_interface;
private static final String ACTION_SHOWACT = "android.intent.action.NEW_OUTGOING_CALL";
#Override
public void onCreate()
{
super.onCreate();
this.context = getApplicationContext();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
final IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_SHOWACT);
this.br_interface = new SecretInterfaceBR();
this.registerReceiver(this.br_interface, filter);
return (START_STICKY);
}
public class SecretInterfaceBR extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Intent intent1 = new Intent(context, ActMain.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent1);
}
}
#Override
public IBinder onBind(Intent intent)
{
return (null);
}
public void onDestroy()
{
this.unregisterReceiver(this.br_interface);
}
}
Thanks for your help.
Could anyone explain me why I have to launch the activity in order it to work ?
Because manifest-registered receivers will not work while the app is in a force-stopped state, as of Android 3.1. That occurs:
On first install
After the user taps the "Force Stop" button in Settings
How to avoid it ?
Only support Android 3.0 and below. Note that this is a diminishing percentage of the Android device market.
Could anyone explain me why I doesn't work and how to correct it ?
See above.
Is this the correct way to do "hide" my application ?
What you are describing does not hide an application, but merely represents an application with no launcher icon. Such applications typically are not useful as of Android 3.1, due to the aforementioned force-stopped restriction. And, these applications are still listed everywhere else that chooses to list applications, such as the Settings app.
I removed the previous lines (question "2) and add the following one in the receiver section (of BOOT_COMPLETED action) in my manifest file but it still doesn't change anything. Any clue ?
A <category> is rarely used with a receiver. The HOME category is used with activities and designates a home screen implementation.

Need to start app immediately after the boot completed

I need to know why my app didn't run immediately after booting in android real phone? My app runs but after a few second of booting.
My Code is
public class AutoStart extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
My activity is running, but after few Seconds of the boot completed. Is it Possible to reduce this few second?
I want to run my app immediately. I Don't want to allow user to access the phone.
This can increase you priority but still there would be some delay. Since android first load its OS and the all the other activity starts.
<receiver
android:name=".AutoStart"
android:enabled="true"
android:exported="true"
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I run through this problem too. To anyone still searching for solution:
I want to run my app immediately. I Don't want to allow user to access the phone.
Consider turning your app into home launcher. Make changes in the manifest:
Add to your activity
android:launchMode="singleTask"
Add to intent filter in activity
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
After that it will launch immediately with system, not showing to user anything else.
Android system does lot of work on boot completed.
hence the intent might be delayed. Depending on the phone capabilities, the intent delay times will vary.

Android 3.1 USB-Host - BroadcastReceiver does not receive USB_DEVICE_ATTACHED

I worked through the description and samples for USB host at developer.android.com to detect attached and detached USB-devices.
If I use an intent-filter in the manifest file to start my application when a device is attached, it works perfectly fine: Plug in, device is detected, android asks for permission to start the application, device information is displayed in a table.
The application I'm developing shouldn't be started/finished only if a device is attached/detached (e.g. data management purposes). Also I do not want the open-dialog to pop up if the app is already running. So I decided not to start the activity directly if a device is attached, but to register a BroadcastReceiver, which is (later) supposed to notify the activity if a device is at-/detached. This receiver recognizes the detach-action just fine, but not the attach-action.
Am I missing a permission or data attribute or something like that? The tutorial and samples don't say anything about additional necessary attributes.
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="de.visira.smartfdr"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="12" />
<uses-feature android:name="android.hardware.usb.host" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".usb.Detector">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
android:resource="#xml/device_filter" />
</receiver>
</application>
And the receiver:
public class FDRDetector extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(context, "Action: " + action, 3).show();
// pops up only if action == DETACHED
}
I don't understand why the same intent-filter works, if I use them on an activity, but not if they are applied to a receiver? Even if I set up the receiver and filter in code, attaches are not recognized.
My work environment:
IDE: Eclipse 3.7 with Android Plugin
Device: Acer Iconia Tab A500
Android: 3.1
Thanks in advance
Aha! I figured it out. I was having the exact same problem.
The gist of it is - if you have your application launch automatically when a device is plugged in (using the manifest file), then it appears the Android system gets the ACTION_USB_DEVICE_ATTACHED intent, and then since it knows your application wants to run in that situation, it actually sends your application the android.intent.action.MAIN intent. It never sends the ACTION_USB_DEVICE_ATTACHED action to your application because it thinks it already knows what your application wants to do in that situation.
I've just now identified the problem, and I think I have a solution, but I can tell you what I've found:
Even if your app is running and in the foreground, when you plug in the USB device and the Android system gets the ACTION_USB_DEVICE_ATTACHED intent, it will call onResume() in your activity.
Unfortunately, you cannot just do this:
#Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
Log.d(TAG, "intent: " + intent);
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
//do something
}
}
Because the intent will come back as android.intent.action.MAIN, NOT ACTION_USB_DEVICE_ATTACHED.
An annoyingly, you also get android.intent.action.MAIN if you just leave the app, but don't unplug USB. I imagine putting the device to sleep and waking it back up will do the same thing.
So from what I have found, you can't get the intent directly, but it does appear that you can rely on onResume() being called when a USB device is plugged in, so the solution is to just check to see if USB is connected every time you get an onResume. You can also set a flag when USB is disconnected, because of course the USB disconnect intent fires just fine.
So in total, your broadcast receiver might look like this:
// BroadcastReceiver when remove the device USB plug from a USB port
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
usbConnected=false;
}
}
};
You'd have this inside of onCreate:
// listen for new devices
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbReceiver, filter);
This goes inside of the activity tag in your manifest:
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
You'll have a device_filter.xml file in your /res/xml/ folder that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1027" product-id="24577" />
<usb-device vendor-id="1118" product-id="688" />
</resources>
(of course with whatever vendor IDs and product IDs you need)
And then your onCreate looks something like this:
#Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
Log.d(TAG, "intent: " + intent);
String action = intent.getAction();
if (usbConnected==false ) {
//check to see if USB is now connected
}
}
I don't have specific code for checking to see if USB is connected as I actually haven't delved into that yet. I'm using a library that will just connect if it can, so for my application I can just start that loop and I'm good.
Its also probably important to set the launchmode of your activity in the manifest to "singleTask" to prevent it from running again when its already running, or else plugging in a USB device will just launch a second instance of your application!
So my whole activity tag in my manifest looks like this:
<activity
android:label="#string/app_name"
android:name="com.awitness.common.TorqueTablet"
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask"
>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</activity>
Anyway, I hope this helps someone! I was surprised that I was unable to find a solution for this already!
Just to follow on from #Gusdor's insightful comment (+1): I implemented a check in onNewIntent() that, as #Gusdor points out, is called when your activity launchMode is set as singleTask or singleTop. Then, rather than checking for boolean flags as the accepted answer suggests, simply pass on the intent to your USB broadcast receiver using a LocalBroadcastManager. For example,
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(intent.getAction())) {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Then, wherever you're registering your existing (system) USB broadcast receiver, just register the same receiver with a local broadcast manager instance, i.e.,
#Override
protected void onResume() {
super.onResume();
myContext.registerReceiver(myUsbBroadcastReceiver, myIntent); // system receiver
LocalBroadcastManager.getInstance(myContext).registerReceiver(myUsbBroadcastReceiver, intent); // local receiver
}
#Override
protected void onPause() {
super.onResume();
myContext.unregisterReceiver(myUsbBroadcastReceiver); // system receiver
LocalBroadcastManager.getInstance(myContext).unregisterReceiver(myUsbBroadcastReceiver); // local receiver
}
You could send another system broadcast rather than a local broadcast, but I don't think you'll be able to use the action UsbManager.ACTION_USB_ACCESSORY_ATTACHED (system would see that as potential security risk), so you'd have to define your own action. No big deal, but why bother, especially as there's no IPC overhead with local broadcasts.
Creating the broadcast receiver within the application, and not the manifest, allows your application to only handle detached events while it is running. This way, detached events are only sent to the application that is currently running and not broadcast to all applications.

Android, Start App on Launch of Emulator

I want to do some testing on the Android emulator to run an app of mine automatically and pass it a parameter how can I do this?
For now lets assume, I have an app that opens a URL (parameter) in a browser. How can I get my emulator to do this upon launch of app?
This is for testing and this app will not be give to other to be used.
Thanks all
You want to add the "boot completed" permission, then add an intent for the broadcast and make a broadcastreceiver:
--in your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
--and also in the manifest:
<receiver android:name=".receiver.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
--then make a receiver:
public class MyBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//use an intent to stat your activity here!
}
}

Categories

Resources