I am trying to send a string from service using broadcast receiver.
On reaching a location I want to send broadcast receiver but broadcast receiver is not able to send anything and nor I am getting any error in Logcat.Also I am not able to receive any error in both activity or service.
Following is my code in service class:-
public class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
inte.setAction("hello");
inte.putExtra("StringFromService", genre);
inte.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(inte);
}
Receiver inside another class:-
public class XYZ extends ListActivity {
public BroadcastReceiver myBR= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String x= intent.getAction();
Log.d("INside BroadcastReceiver", "inside" + x);
if(x.equals("hello")){
Toast.makeText(XYZ.this,"hello", Toast.LENGTH_LONG).show();
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
registerReceiver(myBR, new IntentFilter("hello"));
}
}
To trigger a broadcast, you have to register the receiver in the onResume() method like this:
registerReceiver(myBR, new IntentFilter("hello"));
and unregister the broadcast in the onPause() method
unregisterReceiver(myBR);
Related
Here is bootstartup.java file. I have checked that TestService is working properly. But I just want to know how can I check that data is broadcasted?
Code
public class bootstartup extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// start the service
Intent service=new Intent(context,TestService.class);
context.startService(service);
Log.e("com.example.myglobalbroadcastreceiver","Broadcast Received:"+intent.getAction());
// start the app
Intent app= new Intent(context,Abhijeet.class);
context.startService(app);
}
}
I have written a simple broadcast receiver responding to TIME_TICK action .
When I add the action in the manifest file it is not calling the registered receiver but when I register the receiver in the java code it is being called.
I have a simple onreceive method.
public class mybroad extends BroadcastReceiver
{
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("got", "broadcasted");
Toast.makeText(arg0, "hurray broadcast got", Toast.LENGTH_LONG).show();
}
}
receiver tag for manifest file
<receiver android:name="com.example.chapbasic.mybroad" >
<intent-filter>
<action android:name="android.intent.action.TIME_TICK"></action>
</intent-filter>
</receiver>
when I operate with the following code it is working
public class broadact extends Activity
{
IntentFilter ii;
mybroad mb;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mybroad);
ii=new IntentFilter("android.intent.action.TIME_TICK");
mb=new mybroad();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
registerReceiver(mb, ii);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(mb);
}
Kindly update why it is not being called from the manifest file registration.
thanks
kindly go through the documentation that states.
You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver() .
That is why you are not able to receive it when doing through manifest file.
thanks
Hi I developed one small android application in which I am using one activity one intent service and one broadcast receiver.
So my code looks like :
public class Main_Activity extends Activity {
private ResultReceiver resultReciver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_);
Log.i("***************************88", "inside activity on create");
IntentFilter filter = new IntentFilter("com.nilkash.broadcast.receiver");
resultReciver = new ResultReceiver();
registerReceiver(resultReciver, filter);
//LocalBroadcastManager.getInstance(this).registerReceiver(resultReciver, filter);
Intent intent = new Intent(this, ExampleService.class);
startService(intent);
}
public class ResultReceiver extends BroadcastReceiver{
public ResultReceiver()
{
}
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.i("**********************", "inside broadcast receiver: ");
}
}
}
And intent service
public class ExampleService extends IntentService{
public ExampleService(String value)
{
super(value);
}
public ExampleService()
{
super("");
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Log.i("********************************", "inside intetn reciver: ");
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.nilkash.broadcast.receiver");
//broadcastIntent.putExtra("value", "nilkash");
sendBroadcast(intent);
//LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
In manifest file I define service.
So my problem is that I start service from activity and its working fine. From service on intent receive I sent one broadcast receiver but it not listening inside my broadcast receiver.
Am i doing some thing wrong? Need Help. Thank you.
There is an error: sendBroadcast(intent);. Should be another intent object (broadcastIntent).
How send broadcast from prefrenceActivity to Activity.prefrenceActivity used for reset data of other activity.
In prefrenceActivity, there is a preference, when user click it a alert box open.If select yes then i want to start broadcast.
Code in Activity for receive broadcast
private BroadcastReceiver objResettedReceiver=new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println(" broadCast receiver. . "+intent);
inc = 0;
initObjects();
}
};
register broadcast on Resume
IntentFilter localIntentFilter2 = new IntentFilter(SettingsActivity.broadcastAction);
this.registerReceiver(this.objResettedReceiver, localIntentFilter2);
unregiseter On pause & destroy Activity
protected void onPause() {
// TODO Auto-generated method stub
unregisterReceiver(objResettedReceiver);
super.onPause();
}
protected void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(objResettedReceiver);
super.onDestroy();
System.out.println("hi.. Activity Destroy.......");
}
Start broadcast
Intent intent = new Intent();
intent.setAction(broadcastAction);
sendBroadcast(intent);
i'm trying to wait for the wifi scan to finish in the broadcast receiver!.
I have 1 Activty (ChooseActivity extends Activity) and 1 class (Scan).
In my Activity i call Scan class to scan wifi and return boolean (true) when finish, here is the code.
My purpose is to separate the scan of my activity because i call scan class in several places.
public class ChooseActivity extends Activity implements OnClickListener{
private int idMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose);
idMap = this.getIntent().getExtras().getInt("ID_MAP");
((Button)this.findViewById(R.id.scan_button)).setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.scan_button:
Scan scan = new Scan();
Toast.makeText(getApplicationContext(), " "+scan.scanHotspots(idMap), Toast.LENGTH_SHORT).show();
break;
}
}
}
And here is my Scan class
public boolean scanHotspots(int idMap){
wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
receiver = new Receiver();
IntentFilter intentFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
context.registerReceiver(receiver, intentFilter);
//HERE I WANT STOP EXECUTION TO WAIT test VAIRABLE CHANGE STATUS
return test;
}
public class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
{
test=true;
}
}
}
}
Thank you in advance.