I want to launch my application through dial pad.I am using the following code. For dial pad to launch application (in Broadcast receiver)
public class HiddenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try{
// Toast.makeText(context,"Number Dialed",1).show();
Intent serviceIntent = new Intent(context,MainActivity.class);
serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
catch(Exception e)
{
Log.d(TAG, ""+e.getMessage());
}
While pressing key through dial pad I want to launch my main activity in which I used the following
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hidden_receiver);
//Intent call here
Intent intent=getIntent();
String message = intent.getStringExtra(MainActivity.TELEPHONY_SERVICE);
//text here
But when I press my code its dialed number disappear but neither dialer pad disappear nor MainActivity launches.
how can this issues be resolved?Help me out.....
Thanks.
Use the BroadcastReceiver as follows:
public class MyOutgoingCallHandler extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Extract phone number reformatted by previous receivers
String phoneNumber = getResultData();
if (phoneNumber == null) {
// No reformatted number, use the original
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
if(phoneNumber.equals("1234")){ // DialedNumber checking.
// My app will bring up, so cancel the broadcast
setResultData(null);
// Start my app
Intent i=new Intent(context,MainActivity.class);
i.putExtra("extra_phone", phoneNumber);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Don't forget to register this receiver in your manifest
<receiver android:name="MyOutgoingCallHandler">
<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"/>
Now, if you are ignoring the number checking in receiver, you'll get dialed number inside your MainActivity,
String phone=getIntent().getStringExtra("extra_phone");
if(!phone.equals(null)){
Toast.makeText(getBaseContext(), phone, Toast.LENGTH_LONG).show();
}
Related
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
Log.i("MyReceiver", "MyAction received!");
}
}
In AndroidManifest.xml (under the application tag)
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="MyAction" />
</intent-filter>
</receiver>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendBroadcast(new Intent("MyAction"));
}
}
MyReceiver.onReceive method is never triggered.
Did I miss something?
I use Android 8.
Then you have to use an explicit Intent, one that identifies the receiver, such as:
sendBroadcast(new Intent(this, MyReceiver.class).setAction("MyAction"));
See Broacast limitations in Android 8 release docs.
In Android 8 onwords
We need to provide the explicite class for handling i.e setcomponent param along with action
Example :
private void triggerBroadCast(String firstFavApp, String secondFavApp) {
Intent intent = new Intent("FavAppsUpdated");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.putExtra("FIRST_FAV_APP", firstFavApp);
intent.putExtra("SECOND_FAV_APP", secondFavApp);
intent.setComponent(new
ComponentName("com.android.systemui",
"com.android.systemui.statusbar.phone.FavAppsChanged"));
Log.i(TAG, "Trigger Fav Apps" + firstFavApp + " " + secondFavApp);
favouriteContract.getAppContext().sendBroadcast(intent);
}
Below Android 8
Only action is enough for receiving Broadcast
void broadCastParkingStates(Context context) {
Intent intent = new Intent("ReverseCameraStates");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.putExtra("PARKING_GUIDE", ReverseCameraPreference.getParkingGuide(context));
intent.putExtra("PARKING_SENSOR", ReverseCameraPreference.getParkingSensor(context));
intent.putExtra("TRAJECTORY", ReverseCameraPreference.getTrajectory(context));
Log.i("BootCompletedReceiver", "Sending Reverse Camera settings states BaordCast");
Log.i("BootCompletedReceiver", "States Parking:Sensor:Trajectory="
+ intent.getExtras().getBoolean("PARKING_GUIDE")
+ ":" + intent.getExtras().getBoolean("PARKING_SENSOR")
+ ":" + intent.getExtras().getBoolean("TRAJECTORY")
);
context.sendBroadcast(intent);
}
If you have multiple receivers, you can send broadcast to all the receivers using only custom action defined in manifest for that you need to add the following flag while sending broadcast
Note: I have used adb to test it on Android 10, you can add it in application
FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000
adb shell am broadcast -a MyAction -f 0x01000000
In Kotlin:
val intent = Intent(this, MyBroadCastReceiver::class.java)
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.action = "my.custom.broadcast"
sendBroadcast(intent)
Change the MainActivity's code as follows:
Intent intent = new Intent(this, MyReceiver.class);
intent.setAction("MyAction");
sendBroadcast(intent);
The string itself does not matter, just need to be the same in all places and unique, I use fully qualified name of the constant.
<receiver android:name="com.mypackage.receivers.MyBroadcastReceiver">
<intent-filter>
<action android:name="com.mypackage.receivers.MyBroadcastReceiver.ACTION_CUSTOM"/>
</intent-filter>
</receiver>
The receiver:
package com.mypackage.receivers;
public class MyBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_CUSTOM = "com.mypackage.receivers.MyBroadcastReceiver.ACTION_CUSTOM";
#Override
public void onReceive(Context context, Intent intent) {
if (ACTION_CUSTOM.equals(intent.getAction())) {
// do custom action
}
}
}
To broadcast the intent:
sendBroadcast(new Intent(MyBroadcastReceiver.ACTION_CUSTOM));
I have an alarm manager whose receiver I had registered in my code. The whole point of having an alarm manager in case of Timer is that it should run in background when in Pause state. Now, do I unregister it in onPause() or in OnDestroy() and will it still run in background and wake up and the receiver would receive it?
EDIT:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyClass.class);
i.putExtra("fromReciever", true);
startActivity(i);
}
I would suggest that you register it in your AndroidManifest.xml file. Example:
<receiver android:name="com.example.android.MyReceiver" >
<intent-filter>
<action android:name="com.example.android.USER_ACTION" />
</intent-filter>
</receiver>
This will keep your receiver registered as long as the application is installed on your device. All you have to do is implement it:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
And you are set.
Additionally, you can check out this tutorial for more information.
Added
If you want to resume your Activity you can add this code in your Receiver onReceive method.
Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("fromOnReceive", true);
context.startActivity(intent);
Then, in your Activity onCreate method, you check if it was called from your Receiver
#Override
protected void onCreate(Bundle savedInstanceState) {
if(getIntent().hasExtras()){
boolean fromReceiver = getIntent().getExtras().getBoolean("fromOnReceive");
if(fromReceiver)
//Do work
}
}
I am trying to create an application that could respond when the power button is pressed. To be more specific, which would respond to it when pressed 2 or 3 times.
For now, I tried the following:
public class SMSKey extends BroadcastReceiver{
static int countPowerOff = 0;
private Activity activity = null;
public SMSKey(Activity activity){
this.activity = activity;
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
countPowerOff++;
}else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
if(countPowerOff == 2){
Intent i = new Intent(activity, SMSOptions.class);
activity.startActivity(i);
}
}
}
}
and in my manifest:
<receiver android:name=".SMSKey">
<intent-filter >
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
</intent-filter>
</receiver>
finally in my MainActivty.java:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
SMSKey mReceiver = new SMSKey(this);
registerReceiver(mReceiver, filter);
Even though this works, it only works for the 1st time, it won't work on the 2nd or 3rd attempt when the power button is pressed. Why is that so ??
And another question: as you can see, I am using this KeyPress event in my MainActivity, which means the application is to be open all the time. Is there any other way that I can implement this without getting into the MainActivity.
This is not even an Android problem. You never reset your countPowerOff variable after you have received the 3 key presses. Even after having done that you must consider adding an alarm that will reset your countPowerOff variable to zero after some small timeout. It will allow you to avoid situations where the user does not intend to interact with your application and just presses the button, but it still gets counted.
As to your second question, try implementing an IntentService.
Here is the solution
public class MyReceiver extends BroadcastReceiver {
private static int countPowerOff = 0;
public MyReceiver (){
}
#Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
Log.e("In on receive", "In Method: ACTION_SCREEN_OFF");
countPowerOff++;
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
Log.e("In on receive", "In Method: ACTION_SCREEN_ON");
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
Log.e("In on receive", "In Method: ACTION_USER_PRESENT");
if (countPowerOff >= 2)
{
countPowerOff=0;
Toast.makeText(context, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_LONG).show();
Intent i = new Intent(context, About.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
}
I am new to android, I have created intent's like this -
<receiver android:name=".IncommigCallListener" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver android:name=".OutgoingCallReciever" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Now i created a service like this -
<service
android:name=".CallLogger"
android:exported="false"/>
Class CallLogger
public class CallLogger extends IntentService {
public CallLogger(String name) {
super(name);
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("service started");
}
}
I don't want to have any activity in my application, i just want to start the service so that it can work in background and receive PHONE_STATE and NEW_OUTGOING_CALL intent.
When i start this application, it doesn't log anything on PHONE_STATE or NEW_OUTGOING_CALL intent.
How can start service in background without using any activity ?
Edit :
public class OutgoingCallReciever extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
and
public class IncommigCallListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String incommingCallNumber = incomingNumber;
System.out.println("incomming call : " + incomingNumber);
break;
}
}
}
Just start service in your BroadcastReceiver's onReceive method. As you are registering BroadcastReceiver in AndroidManifist, It will always listen for Broadcasts even if application is not running (OS will run it for you).
Example
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
EDIT
To start a service on Boot completed you can do something like this.
1) Add permission to your Manifist :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2) Register your Broadcast Receiver with BOOT COMPLETE action.
<receiver android:name="com.example.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3) In BootBroadcastReceiver.java:
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent );
}
}
You should be able to do something like this in your receiver.
public class OutgoingCallReciever extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Intent service = new Intent(context, CallLogger.class);
context.startService(service);
}
}
You need to create an intent and call startService() on it to "launch" the service.
Also for what it's worth you should get out of the habbit of System.out.println use Log.d(tag,msg) to print debugging information to the logcat. You can switch the d to other letters if you want to print in different channels.
Why nothing gets printed is only due to the problem that System.out.println does not work in Android! Where do you think the background process will "print" this thing?
You need to change that to Log.d(tag, msg) and then check your logcat to see the output! Otherwise I guess your code might be running properly.
I wanted to know how can i launch my android app with some code from Dialpad. Like if you
##3214789650##
from your galaxy it launches angryGps application.
How to implement that?
Thanks.
try this.use Broadcast Receivers to listen outgoing call number:
Manifest.xml
uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<receiver android:name=".OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
OutgoingCallReceiver.java
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if (null == bundle)
return;
// outgoingNumber=intent.getStringExtra(Intent.ACTION_NEW_OUTGOING_CALL);
String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//START APPLICATION HERE
}
}
What you are looking for is part of contacts application and Although its very likely that the implementation is same for every manufacturer but i am not sure about this.
The intent for starting a new activity is passed by the function handleSecretCode in the file SpecialCharSequenceMgr. The code snippet is
static boolean handleSecretCode(Context context, String input) {
// Secret codes are in the form *#*#<code>#*#*
int len = input.length();
if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Intent intent = new Intent(Intents.SECRET_CODE_ACTION,
Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
context.sendBroadcast(intent);
What you need to do is to register broadcast receiver for the intent action Intents.SECRET_CODE_ACTION and the uri android_secret_code://"code" and in the broadcast receiver you can launch your application.
Also you can see how some applications are already implementing, One of the code that works on emulator is * #* #4636 #* #*.