Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch activity by dialing a number

I've created an appropriate BoradcastReceiver, registered it in Manifest.xml and here is my problem: if my application has already been launched and hanging in background, then dialing a number would bring it to front. If it has not been launched then dialing a number would have no effect.
How can I fix this? I test this on Xiaomi Mi4 with MIUI6 if that's important.

Here's the code (I use Scala):

manifest.xml:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
...
<receiver android:name="DialerGate" android:enabled="true" android:exported="true">
            <intent-filter android:priority="1">
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>

BroadcastReceiver:

class DialerGate extends BroadcastReceiver {
  def onReceive(context: Context, intent: Intent) =
    if (intent.getAction equals Intent.ACTION_NEW_OUTGOING_CALL) {
      val phoneno = intent.getExtras getString Intent.EXTRA_PHONE_NUMBER
      val prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
      val number = prefs.getString(AbstractKit.LAUNCH_NUMBER, null)

      Log.d("WALLET-PHONE", s"Dialed number: $phoneno, saved number: $number")
      Log.d("WALLET-PHONE-OK", (number == phoneno).toString)

      val i = new Intent
      i.setClassName("com.app.wallet", "com.app.wallet.MainActivity")
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)

      val appContext = context.getApplicationContext
      appContext.startActivity(i)


      //if (number == phoneno) context startActivity new Intent(context, target)
      //context stopService intent
    }
}
like image 264
src091 Avatar asked Aug 25 '15 09:08

src091


3 Answers

From a simple user perspective, that cannot be done (its a security feature).

Starting from HONEYCOMB Android doesn't allow any broadcast receivers to be invoked until application is run at least once.

Its basically simpler to allow the program to be run at least once (during boot its the most common one), and then have the intent close the app if its not the time to use it.

Check this for further details on how to implement additional receivers that may do what you need it to do.

like image 81
Bonatti Avatar answered Nov 19 '22 02:11

Bonatti


create a Listener in your Broadcast Receiver and listen to ON_BOOT_COMPLETED, then start your app, in silent mood and you will be resolved to your normal workings.

side note If Activities was to be waken up that way, then Keylogging apps and hacking apps will be very very very cheap to create - hence make android vulnerable.

like image 27
Elltz Avatar answered Nov 19 '22 02:11

Elltz


http://android-developers.blogspot.in/2013/05/handling-phone-call-requests-right-way.html

Listening for outgoing call requests

Apps that provide phone calling services (such as VOIP or number management) can set up Intent filters to handle outgoing call requests, such as those made from the Dialer or other installed apps. This provides a seamless integration for the user, who can transition directly to the calling service without having to redial or launch another app.

When the user initiates a call, the system notifies interested apps by sending an ordered broadcast of the NEW_OUTGOING_CALL Intent, attaching the original phone number, URI, and other information as extras. This gives apps such as Google Voice and others a chance to modify, reroute, or cancel the call before it’s passed to the system’s default phone app.

If you want your phone calling app to be able to handle outgoing call requests, implement a broadcast receiver that receives the NEW_OUTGOING_CALL Intent, processes the number, and initiates a call as needed. Make sure to declare an intent filter for NEW_OUTGOING_CALL in the receiver, to let the system know that your app is interested in the broadcast. You’ll also need to request the PROCESS_OUTGOING_CALLS permission in order to receive the Intent.

Note that the system broadcasts NEW_OUTGOING_CALL only for numbers that are not associated with core dialing capabilities such as emergency numbers. This means that NEW_OUTGOING_CALL can not interfere with access to emergency services the way your use of CALL_PRIVILEGED might.

Here’s an example broadcast receiver declared in an app’s manifest file:

<manifest>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />  
    <application>
        ...
        <receiver android:name=MyOutgoingCallHandler">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        ...
    </application>
</manifest>

And I am sure that <category android:name="android.intent.category.DEFAULT" /> will do the trick for you. check this question too for more details about category tag.here

like image 2
Ansal Ali Avatar answered Nov 19 '22 03:11

Ansal Ali