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);
Related
Hi i created broadcast receiver which receive Battery level on Intent.ACTION_BATTERY_CHANGED event. It works good but when i remove this app from ram using swipe from holding home button then it doesnt receive event.
My code of Broadcast is
private BroadcastReceiver BatteryReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
Toast.makeText(BatteryIndicatorService.this,""+level, Toast.LENGTH_SHORT).show();
}
};
This can be done when your broadcast run in background for that you need to create service.In this service you have to define your Broadcast.
BatteryIndicatorService.java
public class BatteryIndicatorService extends Service {
private BroadcastReceiver BatteryReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
Toast.makeText(BatteryIndicatorService.this,""+level, Toast.LENGTH_SHORT).show();
}
};
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
// Register Receiver.
registerReceiver(BatteryReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
And Start it from MainActivity like
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start service in Background
startService(new Intent(this, BatteryIndicatorService.class));
}
Define Service in Manifest.xml in application tag
manifest.xml
<service
android:name="com.ittl.batteryindicator.BatteryIndicatorService"
android:enabled="true" >
</service>
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);
}
}
please before you don't like my question , please read the details .. what i am trying to do is to use a broadcast receiver when screen-off , so i want my app to start if the screen goes off .. here is my broadcast receiver code :
public class BootReceiver extends BroadcastReceiver {
public boolean screenoff;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenoff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenoff = false;
}
Intent intent1 = new Intent(context, ShakeService.class);
intent1.putExtra("screen_state", screenoff);
context.startService(intent1);
}
and here is the service code :
public class ShakeService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stu
return null;
}
#Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new BootReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try{
Intent intent1 = new Intent(getApplicationContext(), SplashScreen.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return START_STICKY;
}
}
and here is my splash screen activity that i call from the service :
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT=3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
new Handler().postDelayed(new Runnable(){
public void run(){
Intent i =new Intent(SplashScreen.this,HomeScreen.class);
startActivity(i);
finish();
}
},SPLASH_TIME_OUT);
}
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
BroadcastReceiver mReceiver = new BootReceiver();
registerReceiver(mReceiver, filter);
super.onResume();
}
#Override
protected void onPause() {
// mSensorManager.unregisterListener(mShakeDetector);
BroadcastReceiver mReceiver = new BootReceiver();
unregisterReceiver(mReceiver);
super.onPause();
}
}
and as you can noticed i have unregistered my receiver but still i keep seeing this in the logcat :
leaked Intent Receiver are you missing a call to unregisterReceiver??
I agree with the method of registering the receiver in onResume() and unregistering it in onPause(). This helps when the app goes in and out of scope. I'm somewhat confused on what you're trying to do with your app, but initially I see that you create a new instance of the braodcast receiver in onPause() and unregister that. Try unregistering using the same instance . Also, if you want your service to remain running in the background, you may want to look into implementation of a partial wake lock, that keeps the CPU running for your app while the screen remains off. Could you provide one more details about the purpose of the app?
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).
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);