launching application through dialer pad - android

I am learning to launch my application through dialer pad.I am using the following code.
for dialer pad to launch application
(in Broadcast receiver)
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if (null == bundle)
return;
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//here change the number to your desired number
String compare_num="5556";
if(phoneNumber.equals(compare_num))
{
setResultData(null);
// Guardian.changeStealthMode(context,PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
//Intent to move to next Activity
Intent myintent=new Intent(context,MainActivity.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myintent);
// abortBroadcast();
and the launching app containing the main activity
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Intent call here
Intent intent=getIntent();
String message = intent.getStringExtra(MainActivity.TELEPHONY_SERVICE);
//text here
EditText et = (EditText) findViewById(R.id.editText1);
//Button here
Button Login = (Button) findViewById(R.id.button1);
}
while I am launching this code my app don't launch through dialerpad.
Help me please.........

Android class-
public class SecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try{
Toast.makeText(context,"Number Dialed",1).show();
Intent serviceIntent = new Intent(context,CalledClass.class);
serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
catch(Exception e)
{
Log.d(TAG, ""+e.getMessage());
}
}
}
Android manifest-
<receiver android:name=".SecretCodeReceiver" android:enabled="true"
>
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="111" />
</intent-filter>
</receiver>
When user will dial
*#*#111#*#*
then the required event will fire. If you have have requirement to fire event on dial of number then need to implement ACTION_NEW_OUTGOING_CALL as mentioned in answer by "Shyam".

try this
public class Example extends BroadcastReceiver
{
#Override
public void onReceive(Context context, final Intent intent) {
if (intent.getAction().equals(android.intent.action.NEW_OUTGOING_CALL)) {
String phoneNumber = intent.getExtras().getString( android.intent.extra.PHONE_NUMBER );
if(phoneNumber.equals("#1234#")) {
Intent intent1 = new Intent(context , YourActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent1);
}
}
}
}
in your manifest file
<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>

Related

How to send data from Main Activity to BroadcastReceiver?

I know that exists thread about that, but I can't get how to do it, I only need send data a Broadcast, this is the code
Main activity
public void passData(){
passIntent = new Intent(this,Notification_Reciever.class);
path=String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath());
passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);
}
BroadCast
public class Notification_Reciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
String value=intent.getStringExtra("KEY");
if(Constantes.ACTION.PLAY_ACTION.equals(action)){
actionPlay();
Toast.makeText(context,""+value,Toast.LENGTH_SHORT).show();
}
if(Constantes.ACTION.PREV_ACTION.equals(action)){
}
if(Constantes.ACTION.NEXT_ACTION.equals(action)){
}
}
Manifest
<receiver android:name=".Notification_Reciever">
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="PLAY_ACTION"/>
</intent-filter>
</receiver>
Always get null
Since you have intent-filter defined in manifest,
<intent-filter>
<action android:name="PLAY_ACTION"/>
</intent-filter>
You need to set action to your intent in order to receive data...
passIntent = new Intent(this,Notification_Reciever.class);
passIntent.setAction("PLAY_ACTION");
path = String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath());
passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);
You have no set an action to the intent which you are broadcasting.
passIntent = new Intent("action");
In your case 'passIntent = new Intent(Constantes.ACTION.PREV_ACTION);` or whichever the action that you want to broadcast.
You can also use intent.setAction(action) to set an action.
you need to set action here.In manifest it is already registered
public void passData(){
passIntent = new Intent(this,Notification_Reciever.class);
passIntent.setAction("PLAY_ACTION"); path=String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath();passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);
}
Here is an example of broadcast. do it like this
Intent intent = new Intent("IncomingChat");
intent.putExtra("visitor", String.valueOf(eventParameters[0]));
intent.putExtra("Action", "receivedincommingchats");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
and add this to the activity where you want to receive the broadcast
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(IncomingBroadcastReceiver, new IntentFilter("IncomingChat"));
}
private final BroadcastReceiver IncomingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String Action = intent.getStringExtra("Action");
switch (Action){
case "receivedincommingchats":
String recVisit = intent.getStringExtra("visitor");
break;
default:
break;
}
}
};
Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
intent.putExtra("data", "data");
sendBroadcast(intent);
your manifest file should have something like this ..
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.example.Broadcast" >
</action>
</intent-filter>
</receiver>
You have to register your broadcast receiver in your activity..
IntentFilter filter = new IntentFilter("com.example.Broadcast");
MyReceiver receiver = new MyReceiver();
registerReceiver(receiver, filter);
And your broadcast receiver will be like this
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Implement code here to be performed when
// broadcast is detected
}
}

Broadcastreceiver not receiving?

I am trying to pass values from service to activity using broadcast
I am using following code to call broadcast in service
Intent i = new Intent();
i.putExtra("test",result);
sendBroadcast(i);
And receiving in main activity using following code
public class myreciver extends BroadcastReceiver{
public String data =null;
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String datapassed = intent.getStringExtra("test");
}
}
In Main Activity
myreciver m = new myreciver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(m, intentFilter);
but my receiver is not called.
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pragadees.restex" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MainActivity$myreciver" >
</receiver>
<service
android:name=".MyIntentService"
android:exported="false" >
</service>
<service
android:name=".MyService"
android:enabled="true"
android:exported="false" >
</service>
<activity
android:name=".display"
android:label="#string/title_activity_display" >
</activity>
</application>
</manifest>
Action missing in Intent which is passing to sendBroadcast method.do it as:
Intent i = new Intent(MyService.MY_ACTION); //<< pass Action to Intent
i.putExtra("test",result);
sendBroadcast(i);
use broad cast like this
Intent i = new Intent("Broadcastname");
context.sendBroadcast(i);
and now receive broad cast like this way
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter("Broadcastname");
BroadcastReceiver Receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// to your work here
}
});
}
};
this.registerReceiver(Receiver, intentFilter);
finally unregister in onstop() method
#Override
protected void onStop() {
// TODO Auto-generated method stub
if (Receiver != null) {
this.unregisterReceiver(this.Receiver);
}
super.onStop();
}
Android's BroadcastReceiver is part of a framework that allows activities and services to send data to one another, even if they belong to separate apps. This is how apps share data with one another, such as when you share a picture from your gallery to Facebook or G+. However, this extensive capability means that you have to be careful about how you filter your requests, which means that it can be harder to just send a quick message from inside your own app.
If you don't need to worry about receiving data from other apps, then you can use the LocalBroadcastManager, which is an implementation of BroadcastReceiver that is confined inside of your own app's jurisdiction. It can't send or receive intents from outside your app. Its interface is nearly identical to BroadcastReceiver's:
public class MyActivity extends Activity {
private LocalBroadcastManager mBroadcastManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBroadcastManager = LocalBroadcastManager.getInstance(this);
//Build an intent filter so you only receive relevant intents
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Test from Service to Activity");
//Register a new BroadcastReceiver with the LocalBroadcastManager
mBroadcastManager.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String dataPassed = intent.getStringExtra("test");
}
}, intentFilter);
//If you ever want to send a broadcast, use this:
Intent sendIntent = new Intent(this, MyService.class);
sendIntent.setAction("Test from Activity to Service");
sendIntent.putExtra("test", "This is a test from Activity!");
mBroadcastManager.sendBroadcast(sendIntent);
}
}
//Then in your Service...
public class MyService extends Service {
private LocalBroadcastManager mBroadcastManager;
public void onCreate() {
mBroadcastManager = LocalBroadcastManger.getInstance(this);
}
public int onStartCommand(Intent intent, int flags, int startId) {
//Build intent filter
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Test from Activity to Service");
mBroadcastManger.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String dataPassed = intent.getStringExtra("test");
}
}, intentFilter);
//To send data to the activity:
Intent sendIntent = new Intent(this, MyActivity.class);
sendIntent.setAction("Test from Service to Activity");
sendIntent.putExtra("test", "This is a test from Service!");
mBroadcastManager.sendBroadcast(sendIntent);
}
}

Listening for ACTION_DREAMING_STOPPED

How would I get my app to listen for when DayDream stops. When the system stops dreaming it sends the ACTION_DREAMING_STOPPED string out. I have added a BroadcastReceiver in my OnResume and onCreate and neither are used when DayDream stops. So where should I put my listener? I do apologize if I am calling something by its wrong name, I haven't worked with DayDream before.
#Override
protected void onResume() {
mDreamingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
// Resume the fragment as soon as the dreaming has
// stopped
Intent intent1 = new Intent(MainActivity.this, MainWelcome.class);
startActivity(intent1);
}
}
};
super.onResume();
}
The BroadcastReceiver can be created in your onCreate.
Ensure you register the receiver with: registerReceiver(receiver, filter) and that you've got the intent-filter inside your AndroidManifest.xml.
Sample:
MainActivity.java
public class MainActivity extends Activity
{
private static final String TAG = MainActivity.class.toString();
private BroadcastReceiver receiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, TAG + " received broacast intent: " + intent);
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
Log.d(TAG, "received dream stopped");
}
}
};
filter = new IntentFilter("android.intent.action.DREAMING_STOPPED");
super.registerReceiver(receiver, filter);
}
}
AndroidManifest.xml
<activity
android:name="com.daydreamtester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

registerReceiver Does not work

I'm trying to deal with push notifications in my main class (and i also have GCMBroadcastReceiver - for all the notifications that comes when i'm not running the main class)
but the registerReceiver Does not work
(GCMBroadcasrReceiver works fine)
my code:
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.google.android.c2dm.intent.RECEIVE"));
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver","Working");
}
};
}
Manifest:
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
*Works fine only in my 4.1.2 (S3)
Well, found the solution:
in my GCMIntentService.java i need to set sendBroadcast like so:
#Override
protected void onMessage(Context context, Intent intent) {
Intent i = new Intent("com.my.app.DISPLAY_PUSH");
i.putExtra("msg", intent.getExtras().getString("msg"));
context.sendBroadcast(i);
}
and the BroadcastReceiver should be
protected void onCreate(Bundle savedInstanceState) {
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.my.app.DISPLAY_PUSH"));
}
.
.
.
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver","Working with msg:" + intent.getExtras().getString("msg") );
}
};
I wonder why it works in 4.1.2 without the sendBroadcast...
if you call sendBroadcast like this
Intent intent = new Intent(context, mBroadcastReceiver.getClass());
intent.setAction(ACTION_ON_CLICK);
context.sendBroadcast(intent);
// or
Intent intent = new Intent(context, MyBroadcastReceiver.class);
intent.setAction(ACTION_ON_CLICK);
context.sendBroadcast(intent);
change it to this :
Intent intent = new Intent(ACTION_ON_CLICK);
context.sendBroadcast(intent);

Android broadcast receiver and intent filter

I am new to android platform.please help me out how the Broadcast Receiver and Intent Filter behaves in android.
Is there any other way to broadcast other than
public class OutGoingBrodcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.err.println("OUTGOING");
Intent i=new Intent();
i.setAction("com.ith.OUT");
context.sendBroadcast(i);
}
}
to receive broadcast in
public class IncomingBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("com.ith.OUT")){
System.out.println("hit INCOMING");
}
}
}
mainfest
<receiver android:name=".OutgoingReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
</intent-filter>
</receiver>
<receiver android:name=".IncomingReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.ith.OUT"></action>
</intent-filter>
</receiver>
i want to know is there any other way to broadcast other than
Intent i=new Intent();
i.setAction("com.ith.OUT");
context.sendBroadcast(i);
using custom intent filter like com.ith.OUT

Categories

Resources