src091
src091

Reputation: 2847

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

Upvotes: 10

Views: 2821

Answers (6)

Gautam Jain
Gautam Jain

Reputation: 671

The application might not have the permissions to the "phone", either ask for permissions at runtime or go to application settings and enable all the permissions asked by the application.
This worked for me..

Upvotes: 0

KISHORE_ZE
KISHORE_ZE

Reputation: 1466

You could try a few things.

Try using java, if not try the following.

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

Changed priority and removed unnecessary stuff.

Also though I am good at Broadcast Receivers I don't have experience in Scala, so I can only suggest a few ideas. Remove the if statement. It is not required as you have already have an <intent-filter>. Also change the intent as in the paste bin code.

Upvotes: 2

Ansal Ali
Ansal Ali

Reputation: 1603

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

Upvotes: 2

Elltz
Elltz

Reputation: 10859

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.

Upvotes: 2

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

Have a look here. You may use a service, but just care about one thing: when the app is closed the service get closed also because they are in a one thread, so the service should be on another thread in order fot it not to be closed You can keep it alive with AlarmManager. In the link there are also some samples :)

Hope it helps!

Upvotes: 1

Bonatti
Bonatti

Reputation: 2781

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.

Upvotes: 5

Related Questions