Hi I am using this code to call a Service from BroadCastReceiver but its not working.
here is the code:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("luli");
Intent myIntent=new Intent(context,AlarmReceiver.class);
// myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(myIntent);
}
}
then on my Service I have this code:
public class AlarmService extends Service {
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("HEY u bastard service print at least something");
return null;
}
}
I have also declared in Manifest the service
What is wrong here that is not responding the Service??
You override onBind(). Thats only called when someone creates a persistent connection to the service via binding (Context.bindService()). You should rather override onStartCommand().
Related
Hi I am new to android and it is also my first time I am going work with service which will work in the background.
I mean I want to build a voice commanding application and I want it to listen the user's command even when it is closed. And I want to start my service at the time of pressing the 'Back' button by any user.
I will be so grateful for your great help.
Try this:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class Startappservice extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.enwaye_connect.MainActivity");
startActivity( LaunchIntent );
}
To start service on clicking back button:
Intent start= new Intent(this, Startappservice .class);
startService(start);
Add in your manifest:
<service android:name="your_package_name.Startappservice" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="our_package_name.Startappservice" />
</intent-filter>
</service>
You have to use the Service class. Create a class that derives from it and then you can add your methods to the service.
public class MyService extends Service {
// This is used to establish a communication with the service.
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
// Called when the service is created
#Override
public void onCreate() {
// YOUR CODE
}
// Called when the service is started
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// YOUR CODE
return START_STICKY;
}
// called when the service instance is destroyed
#Override
public void onDestroy() {
// YOUR CODE
}
// Returns the binder which is used for communication with the service.
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
To start the service use:
Intent start= new Intent(this, MyService.class);
startService(start);
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 can get the BroadcastReceiver to work but I'm not sure why the code for the intent to start the service has an error.
public class BroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent backGround = new Intent(this,BackGround.class);
startService(backGround);
}
}
The error's are with the new intent(this,BackGround.class) and startService(); now I think I need to implement the service class for the startService method and (this) needs to be something else but I'm not sure what to do here.
This should do it!
public class BroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, BackGround.class));
}
}
and don't forget to include service in manifest as well.
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 have an Android application which uses C2DM services (aka push).
I have a separate class which implements the registration process and which receives the data (and extends BroadcastReceiver).
I want to communicate this data to the activity which currently is in the foreground. The activity currently in the foreground may differ depending on user action.
What's the best way to communicate in between the receiver and the current activity?
Thanks.
I solved this problem by sending out a new broadcast from the C2DMReceiver class, which looked something like this.
The C2DMReceiver class:
public class C2DMReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
handleRegistration(context, intent);
} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(context, intent);
}
}
private void handleRegistration(Context context, Intent intent) {
// handle registration
}
private void handleMessage(Context context, Intent intent) {
Intent i = new Intent("push");
i.putExtras(intent);
// context.sendOrderedBroadcast(i, null);
context.sendBroadcast(i);
}
}
Another class I called PushReceiver. This is the class that will extend BroadcastReceiver and receive the broadcast sent by C2DMReceiver.
public class PushReceiver extends BroadcastReceiver {
public PushReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
// do stuff
abortBroadcast();
}
public static class PushFilter extends IntentFilter {
private static final int DEFAULT_PUSH_PRIORITY = 1;
public PushFilter() {
this(DEFAULT_PUSH_PRIORITY);
}
public PushFilter(int priority) {
super("push");
setPriority(priority);
}
}
}
And the activity class, in this case called MyActivity. This should work well if you are using a base activity class that all other activities extend. That way every activity registers the receiver. By doing the register/unregister in onResume/onPause, you should be able to guarantee that only the current activity receives the broadcast. If not, you can send an ordered broadcast from C2DMReceiver and use priority in the PushFilter.
public class MyActivity extends Activity {
private PushReceiver pushReceiver;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your onCreate method
pushReceiver = new PushReceiver();
}
public void onResume() {
super.onResume();
// your onResume method
registerReceiver(pushReceiver, new PushReceiver.PushFilter());
}
public void onPause() {
super.onPause();
// your onPause method
unregisterReceiver(pushReceiver);
}
}
In my case, I wrote the PushReceiver constructor to take a View and then "did stuff" with the view in the onReceive method. Without knowing more about what your trying to do, I can't elaborate on this, but hopefully this can provide a decent template to work from.