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
Related
I tried to register the broadcast receiver in the manifest but the onReceive method isn't invoked. What am I doing wrong?
manifest.xml
<receiver
android:name="zzz.yyy.xxx.ui.storyboard.discussion.DiscussionFragment$MessageCountReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="zzz.yyy.xxx.ui.storyboard.discussion.GROUPCHAT" />
</intent-filter>
</receiver>
Sending broadcast as below:
Intent intent = new Intent("zzz.yyy.xxx.ui.storyboard.discussion.GROUPCHAT");
intent.putExtra(BundleKeys.notification_post_id_forum,postId);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Receiving broadcast in Fragment class as below:
#Override
public void onPause() {
clearUnreadCount();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(messageRefresher);
super.onPause();
}
public class MessageCountReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String noti_post_id = intent.getStringExtra(BundleKeys.notification_post_id_forum);
int tempCount=PreferencesIMDStar.getIntValue(getActivity(),noti_post_id);
tempCount++;
PreferencesIMDStar.setIntValue(getActivity(),noti_post_id,tempCount);
if (getActivity() != null && noti_post_id.equalsIgnoreCase(post_id)) {
discussionPresenter.fetchCommentReply(post_id, true);
}
}
}
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
}
}
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);
}
}
I'm using android(version 4.1.1) MeLE box(SmartTv) for developing one application, i need to start up my application when device Boot Time is completed but my device not catch up the BOOT_COMPLETED Action. If i'm use that same application in mobiles or emulator the Boot_Completion action were caught by Broadcast_receiver.
if anyone known about this issue help me thanks in advance....
here is my code...
manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="NotifyingDailyService" >
</service>
BootCompletedReceiver class:
public class BootCompletedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.w("boot_broadcast_poc", "starting service...");
context.startService(new Intent(context, NotifyingDailyService.class));
}
}
Service class:
public class NotifyingDailyService extends Service {
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");
return super.onStartCommand(intent, flags, startId);
}
}
One thing I noticed is you don't have the category set for your receiver in your Manifest. The following works for me in my App.
<receiver android:name="us.nineworlds.serenity.StartupBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Then in my StartupBroadcastReceiver, i have the following
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) {
return;
}
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean startupAfterBoot = prefs.getBoolean("serenity_boot_startup", false);
if (startupAfterBoot) {
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
This will start the MainActivity class once bootup has been completed.
Link to the project code is here: https://github.com/NineWorlds/serenity-android/blob/master/serenity-app/src/main/java/us/nineworlds/serenity/StartupBroadcastReceiver.java
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>