10

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
    }
}
src091
  • 2,807
  • 7
  • 44
  • 74
  • Can't you just have your app run on boot? http://stackoverflow.com/questions/10428510/how-to-start-launch-application-at-boot-time-android/10428665#10428665 – KXIII Aug 25 '15 at 10:29
  • @JesterXiii, no, that's not what I want at all. My goal is to launch an app dialing a specific number. – src091 Aug 25 '15 at 10:51
  • @Anton The way broadcasts work when they are registered in the Manifest is such that they will be called to wake the process if its not running. – JoxTraex Aug 27 '15 at 09:58
  • this might help - http://stackoverflow.com/questions/17122085/starting-the-application-from-a-broadcastreceiver-new-outgoing-call-doesnt-alw – dhaval Aug 27 '15 at 10:06
  • You mean that if the app is in background it works, if not it doesn't? – Pier Giorgio Misley Aug 27 '15 at 15:28
  • @PierGiorgioMisley yes, that is exactly what happens. – src091 Aug 27 '15 at 16:07

6 Answers6

5

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.

Community
  • 1
  • 1
Bonatti
  • 2,778
  • 5
  • 23
  • 42
  • Hmm, it's not quite the problem I'm experiencing as I did launch an app many times before dialing a number. – src091 Aug 27 '15 at 15:26
  • @Anton Just some details... I did not use the def, rather preffer to use the full `@Override public void onReceive(Context c, Intent i){stuffs()}` versions, since that will be easier to follow on program updates... Also, you are full of "typos" in your code... since there are almost no `;`, as well as no function calls to functions `val i = new Intent` is not java.... `int i = new Intent();` could be – Bonatti Aug 27 '15 at 15:39
  • I'm sorry I've forgot to mention I use Scala. I have just updated the post. – src091 Aug 27 '15 at 16:08
  • It's recommended to use java. I have no idea in Scala. Since a lot of devs including me use java it's a lot easier for us to help you if you use java. – KISHORE_ZE Sep 02 '15 at 14:02
  • @Anton to help out more than just criticising or so I modified your code to java. (WITH A FEW CHANGES) its located at http://pastebin.com/f3ma6zQ7 – KISHORE_ZE Sep 02 '15 at 14:12
2

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.

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • in other words, this behavior of not launching an app if it's not already hanging in background is by design? – src091 Aug 30 '15 at 18:42
2

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

Community
  • 1
  • 1
Ansal Ali
  • 1,583
  • 1
  • 13
  • 30
2

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.

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
1

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!

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • this is incorrect sir, service is not dependent on Activity life cycle which means unless you explicitly kil it. – Elltz Aug 30 '15 at 20:19
0

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

Gautam Jain
  • 651
  • 8
  • 16