TIME_TICK action not calling broadcast receiver - android

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

Related

Android Broadcast not execute when clear from ram

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>

Android Service's method onDestroy called before Bluetooth scanning ends

I have a problem that I start a service with an Intent, in onCreate() method register BroadcastReceiver and then in onDestroy() unregister the BroadcastReceiver. In Method onHandleIntent() I start discovery for bluetooth devices. THe problem is, that I my BroadcastReceiver is destroyed before any device is found so I cant really do anything with the BluetoothDevice.ACTION_FOUND intent. I tried to move startDiscovery() to onCreate() but no results. My code looks like this:
public class BluetoothService extends IntentService{
private BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
private final String SMART_TOKEN_ADDRESS = "F0:E7:7E:5F:63:70";
private final BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v("BLUETOOTH","found it!");
}
};
public BluetoothService() {
super("BluetoothService");
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(){
super.onCreate();
Log.v("SERVICE", "Just got created");
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
btAdapter.startDiscovery();
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Log.v("BLUETOOTH", "Discovery just started");
//btAdapter.startDiscovery();
}
#Override
public void onDestroy() {
super.onDestroy();
Log.v("BLUETOOTH","Just got destroyed!");
unregisterReceiver(receiver);
}
}
Log looks like this:
1. Just got created
2. Discovery just started
3. Just got destroyed!
4. Received android.bluetooth.device.action.FOUND
Thanks for help!
T.
This is happening because after handling all the requests, the service is stoped. From documentation: "Stops the service after all start requests have been handled, so you never have to call stopSelf()."
So, you should use wait or other command to tell the worker thread to wait the execution, if you want to use service.
Please, refer to the documentation about services: http://developer.android.com/guide/components/services.html

No response after broadcast receiver from service

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);

How start broadcast from prefrence to Activity

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);

Start service when device boot completed

I am doing an application in which I want to call a number when that device boot completed. My code is like this:
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("**inside onRecevier");
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.test.app.TestService");
context.startService(serviceIntent);
}
First I created BroadcastReceiver. I registered this receiver in manifest file like this:
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
In a receiver I called the below service:
public class TestService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("**inside onCreate");
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555"));
startActivity(call);
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("**inside onDestroy");
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
System.out.println("**inside onStart");
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
when I tried to boot a device after booting the application getting force close. How to do this in android??
Thanx in advance
You need to add the NEW_TASK flag before starting an activity from a service:
Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555"));
call.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(call);
This explains:
Note that if this method is being called from outside of an Activity
Context, then the Intent must include the FLAG_ACTIVITY_NEW_TASK
launch flag. This is because, without being started from an existing
Activity, there is no existing task in which to place the new activity
and thus it needs to be placed in its own separate task.
Also, you must hold the permissions:
RECEIVE_BOOT_COMPLETED
CALL_PHONE
And as Waqas mentioned, it would be better for you to start your service from your onReceive like:
Intent serviceIntent = new Intent(context, TestService.class);
context.startService(serviceIntent);
Make sure you've done all that I've stated, and if you continue to have an issue, then it would be helpful if you edit your question and paste the logcat from the force close.
change your onReceive to this
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("**inside onRecevier");
Intent serviceIntent = new Intent(context, TestService.class);
context.startService(serviceIntent);
}
There's a variety of reasons why you'd get a Force Close. The best way to tell which is to look at the log. It should tell you exactly what exception is being thrown, and give you a hint as to how to solve the problem.

Categories

Resources