i try catch a call from standard dialpad using that code:
<action android:name="android.intent.action.CALL" />
<data android:scheme="tel" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.CALL_PRIVILEGED" />
</intent-filter>
</activity>
Everything fine, when user dial from standard phone dialpad, my app opened.
But i don't find solution, how i can get a phone number, which user was dialed. That code inside PhonePadActivity activity onCreate block:
Intent intent = getIntent();
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
gives me a null finally :(
tried to using brodacast receiver:
in manifest:
<receiver
android:exported="true"
android:name="com.myapps.android.DialBroadcastReceiver" >
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
this is class DialBroadcastReceiver:
package com.myapps.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DialBroadcastReceiver extends BroadcastReceiver {
private static final String THIS_FILE = "PhonePadActivity";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e(THIS_FILE,"In onReceive()");
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.e(THIS_FILE,"Number is: "+number);
}
}
}
but logs nod fired, when user press dial
This works for me:
In Manifest:
<intent-filter>
<action android:name="android.intent.action.CALL"/>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.CALL_PRIVILEGED"/>
<data android:scheme="tel"/>
</intent-filter>
In Activity:
String inputURI = this.getIntent().getDataString();
if (inputURI != null) {
Uri uri = Uri.parse(Uri.decode(inputURI));
if (uri.getScheme().equals("tel")) {
String calledNumber = uri.toString();
}
}
You are getting the number with incorrect code. Replace:
Intent intent = getIntent();
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
With:
Uri data = getIntent().getData();
if (data != null && ("tel".equals(data.getScheme()))) {
String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
if (number != null) {
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
}
}
Add <intent-filter android:priority="9999"> to the intent-filter declaration in the manifest, to make sure you're first in line
Remove the com.myapps.android-part from the android:name property of the receiver declaration in the manifest (i.e. it should be: android:name=".DialBroadcastReceiver")
Register a BroadcastReceiver like this in Manifest file:
<!-- DIAL Receiver -->
<receiver
android:exported="true"
android:name="receivers.DialBroadcastReceiver" >
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
You need Permission for NEW_OUTGOING_CALL :
Eventually you can Receive the broadcast and retrieve the dialed number like this:
public class DialBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v("DileBroadCastReceiver","In onReceive()");
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.v("DialBroadcast Receiver","Number is: "+number);
}
}
}
Related
This is what I have so far but nothing happens when I input this combination in dialer
public class DialReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, final Intent intent) {
if (intent.getAction().equals(android.content.Intent.ACTION_NEW_OUTGOING_CALL)) {
String phoneNumber = intent.getExtras().getString( android.content.Intent.EXTRA_PHONE_NUMBER );
if(phoneNumber.equals("*#588637#")) {
Intent intent1 = new Intent(context , Activity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent1);
}
}
}
}
and in androidmanifest
<receiver
android:name=".receiver.DialReceiver"
android:exported="true"
android:process=":background"
tools:ignore="ExportedReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Try with these small changes..
String phoneNumber = intent.getExtras.getString("Intent.EXTRA_PHONE_NUMBER");
if(phoneNumber.equals("*#588637#")) {
//do your stuff
}
And do not forget to add this line in your Manifest.xml file
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Also you may find these helpful..
http://android.programmerguru.com/simple-dialer-application/
http://tikuflower.blogspot.com/2011/12/android.html
Is the receiver getting the broadcast at all? If not, maybe you forgot to include the PROCESS_OUTGOING_CALLS permission.
Try This,
Add to this in manifest,
here host is 12456, so your secret code is *#*#123456#*#* (dial-in dialped)
<receiver android:name=".Dialer"> //here is your broadcast receiver class
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code"
android:host="123456"
/>
</intent-filter>
</receiver>
here is your Broadcast Receiver class :
class Dialer : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// Declare Here your launcher activity in Intent
var i : Intent = Intent(context, MainActivity::class.java)
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context!!.startActivity(i);
}
}
According to ridoy's 2nd link,
http://tikuflower.blogspot.com/2011/12/android.html
It should be
String phoneNumber = intent.getStringExtra("android.intent.extra.PHONE_NUMBER");
rather than
String phoneNumber = intent.getExtras.getString("Intent.EXTRA_PHONE_NUMBER");
That change works for me at least...
I want to develop app that doesn't have icon launcher. The app will run alarmscheduler which triggered when app is installed or phone is rebooted.
The problem is how can I open the activity since the app doesn't have intent filter like below:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is there a way to handle dial code such as ##4635*#*# to open the activity ?
or any other solutions are welcomed.
You can do it with two ways:
1) as answer by #KishuDroid
2) By define code in manifest
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
In manifest file
<receiver android:name=".MySecretCodeReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="4635" />
</intent-filter>
</receiver>
Note:
In Second method you must have to dial *#*#your_code#*#* and dont need to press call button
But in first method you can customise your prefix or postfix of code. For example *#your_code# or **your_code##. but you need to press call button.
You have to use Broadcast Receiver...
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
if(code.equals("#056700") {
intent.setComponent(new ComponentName("com.example", "com.example.yourActivity"));
And Your Android Manifest
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Also, include the permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
I want to write a BroadcastReceiver to receive the application install action. But it failed, so I test if my receiver is well or not. So custom a intent, it also filed. below is my code. Please help me correct it.
public class MyInstallReceiver extends BroadcastReceiver {
// public MyInstallReceiver() {
// }
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
Log.d("receiver", "Intent Detected");
if (intent.getAction (). equals ("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString ();
//System.out.println ("installed:" + packageName + "package name of the program");
Log.d("receiver","installed:" + packageName + "package name of the program");
}
}
}
custom intent
public void installAPK(View v){
startActivity(intent);
Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);
Log.d("receiver", "Intent sent");
}
Manifest.xml
<receiver
android:name=".MyInstallReceiver"
android:enabled="true"
android:exported="true" >
<Intent-filter>
<action android:name = "android.intent.action.PACKAGE_ADDED"/>
<action android:name = "android.intent.action.PACKAGE_REMOVED"/>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
<Data android:scheme = "package" />
</Intent-filter>
</receiver>
enter code here
I don't know about correct spelling in your manifest, but this code is definitely works very well:
<receiver android:name=".MyInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Every application install/uninstall will trigger this receiver.
Everythong looks good, expect a typo in your manifest. It should be <intent-filter> and not <Intent-filter>
In my i have a receiver to predict incoming calls,Once i have the call i need bring my app to front of screen(Over call accept and reject scree), the code for receiver is shown below,
public class IncomingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("IncomingCallReceiver",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("IncomingCallReceiver","State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Intent intent2open = new Intent(context, com.varma.samples.detectcalls.ui.MainActivity.class);
intent2open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent2open.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String name = "KEY";
String value = "String you want to pass";
intent2open.putExtra(name, value);
context.startActivity(intent2open);
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
}
in my manifest i added following parameters,
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="com.varma.samples.detectcalls.ui.MainActivity"
android:label="#string/app_name" android:launchMode="singleTop" android:taskAffinity="#android:string/no">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
<receiver android:name="com.varma.samples.detectcalls.receivers.IncomingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
But when i launch the activity the receiver is started to listen incoming calls,then i close all instance of app in Settings, when call is coming for first time, my app is bring front over answer & reject screen.
Then i push back button to reject my call. Then when i make a call again the activity was not bring in front of my dialing screen. I don`t know where is the problem.
<receiver android:name=".ExampleBroadCastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_TIMEZONE_CHANGED"/>
</intent-filter>
</receiver>
package com.broadcastreceiver;
import java.util.ArrayList;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ExampleBroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
// TODO Auto-generated method stub
Log.d("ExmampleBroadcastReceiver", "intent=" + intent);
Intent intent1 = new Intent(context,Login.class);
context.startActivity(intent1);
}
}
I run this above code change the time zone in settings not calling other activity. Can anybody tell what the problem is?
I was having the same issue, and this thread helped me get TimeZone updates working, however I still wasn't getting notifications for Date/Time changes. I finally found that there's a difference in what you specify in your manifest file and what your broadcast receiver uses when filtering intents. While it IS documented in the Android Intent reference, it is very easy to overlook!
In your AndroidManifest.xml file, use the following:
<receiver android:name=".MyReceiver">
<intent-filter>
<!-- NOTE: action.TIME_SET maps to an Intent.TIME_CHANGED broadcast message -->
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And in your receiver class:
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
private static boolean DEBUG = true;
#Override
public void onReceive(Context context, Intent intent) {
final String PROC = "onReceive";
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
if (DEBUG) { Log.v(TAG, PROC + ": ACTION_BOOT_COMPLETED received"); }
}
// NOTE: this was triggered by action.TIME_SET in the manifest file!
else if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)) {
if (DEBUG) { Log.v(TAG, PROC + ": ACTION_TIME_CHANGED received"); }
}
else if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
if (DEBUG) { Log.v(TAG, PROC + ": ACTION_TIMEZONE_CHANGED received"); }
}
}
}
It wasn't working because the intents are wrong. Replace the ones above with:
<intent-filter>
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME" />
</intent-filter>
Then go to the Settings area and change the timezone.
I guess you didn't include the following lines in your AndroidManifext.xml, did you?
<receiver android:name=".ExampleBroadcastReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.ACTION_TIMEZONE_CHANGED" />
<action android:name="android.intent.ACTION_TIME" />
</intent-filter>
</receiver>