I have app that get data from mysql with GetActivity activity and save in mysqlite and create notifications Now how i want my app run automatic and start GetActivity in background to get data
You'll want to look into Services which are threads that run in the background of Android. From within there you'll be able to load your data from your SQL database.
To take care of the automatic starting, you'll want to look into BroadcastReceivers, specifically the BOOT_COMPLETED receiver.
First you need to add this.
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
///// Add your Background Service that Sync your data from database or internet//////////
//// I do the same work it fine work to me.///////
context.startService(new Intent(context, service_ReloadSqlDB.class));
/* Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);*/
}
}
Secondly you need to add this in manifest
<receiver
android:name="com.b2mtech.wrapper.MyBroadcastreceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Hope this help you. Enjoy
http://smrnatore08.wix.com/smr-it-ltd
Related
I want to build a android background service for check data from MySQL data base.Normally I doing extend from Service class and when start the app,I run service using startService() method.But problem is if i remove the app from task manager,the service is also stopped.Another thing is I want to start this service when start the device,I mean beginning.How I implement this.Help me.
When you kill your app, the service is going to restart, not be removed. You can decrale the flag to define the break point when service restart, and write some 'if' 'else' to do thing after this break point.
And if you want to start serivce when start the device, just create the broadcastReceive call 'autoStart'.
In Manifest:
<receiver android:name=".autoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and in autoStart class:
public class autoStart extends BroadcastReceiver
{
public void onReceive(Context ctx, Intent arg1)
{
Intent intent = new Intent(ctx,yourservice.class);
ctx.startService(intent);
Log.i("Autostart", "started");
}
}
When started device, system will detect on boot completed, and call this autoStart BroadcastReceive, and call your service from here
I'm working on an app where it has it's own DB and will be syncing with the backend via GCM, I'm thinking of using background service but I'm not sure if this is the right way to think about it, so, I would really appreciate if you can tell me the below is correct or not, if not can you please state what I need to do in steps or how should think about it? no code is required.
When the app has no running/active activity, assume that GCM has a payload and no need to contact the backend,
1. Backend had new data and sent it with GCM
2. Background service received it and updated the DB
When the app is currently running
1. Backend had new data and sent it with GCM
2. Background service received it and updated the DB and notifydatasetchanged
3. Data on activity will be changed as the source has changed(e.g listview update it's items)
In general your idea is ok. But you don't need to have always running background Service. Just create WakefulBroadcastReceiver and add it into your Manifest:
<receiver
android:name=".receivers.GCMReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.your.package" />
</intent-filter>
</receiver>
Receiver could look like this:
public class GCMReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GCMService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
}
}
This receiver launches your Service(GCMService).
I am working with Android.
I have an app I am working on uses an Activity to setup specific user input values that are then used by a service to provide alerts based on those values. Doing the research I determined how I could get the app to start up when the phone boots, however, what I really want is to have the service start but not have the app load to the screen. Currently the entire app loads to the screen when I turn on the device and then I have to exit out of it.
I have downloaded similar programs that have interfaces for settings but otherwise run in the background. How is that done?
First you have to create a receiver:
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent arg1) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, YourService.class));
}
}
Then add permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register intent receiver:
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this is done, your application (Application class) will run along with services, but no Activities.
Ah, and don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.
So, I have a service that runs in the background (it's super secret so I cant tell you what it is:) ) but I need to be able to shut it off when the user initiates a call or a call is coming in. So far, I have checked out the Intent.ACTION_ANSWER, but for some reason, my Broadcast receiver never detects it. I have also tried to use the PhoneStateListener, but in my case statements, I am failing to understand how to do anything with my service. To what context is a PhoneStateListener?
Some example code for my BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_ANSWER)) {
phoneRinging = true;
}
Intent i = new Intent(context, MyService.class);
i.putExtra("phone", phoneRinging);
context.startService(i);
}
Here is a snippet for starting a service via the PhoneStateListener.
Example: startService(new Intent(---CONTEXT---, MySuperSecretService.class))
And the receiver is in the manifest:
<receiver android:name=".PhoneReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.ANSWER"></action>
</intent-filter>
</receiver>
WTH goes into the CONTEXT portion in this statement if I am in a PhoneStateListener?
Please check ACTION_PHONE_STATE_CHANGED instead of ACTION_ANSWER. ACTION_ANSWER is not broadcast intent for incoming call. It's a standard activity action.
Hey there,
has anyone else discovered that sometimes BOOT_COMPLETED intents arrive out of nowhere?
I have created an OnBootRecoverReceiver which starts a service after it received a BOOT_COMPLETED intent from android - works fine so far... but in some (yet not traceable) events i receive such an intent even though there was no reboot at all.
Anyone has a clue about that, or had the same problem before?
The Manifest entry for the receiver:
<receiver android:name=".trigger.OnBootRecoverReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>`
Receiver Code:
public class OnBootRecoverReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("de.android.stuff.trigger.OnBootRecoverService");
context.startService(serviceIntent);
}
}
If anyone has a idea, please feel free to help.
To clear things up here: there was no BOOT_COMPLETED intent created anywhere
For some reason the Service (which is startet by the Receiver) crashed on the particular device some time ago. Our good friend the android ActivityManager then decided to re-animate the said Service:
03-16 12:00:02.239: WARN/ActivityManager(2504): Scheduling restart of crashed service de.ukn.hci.android.diary/.trigger.OnBootRecoverService in 5000ms
Which of course led to me thinking the Recevier was fired again, but - as it turns out there was just the Service starting again. Without any REBOOT intent whatsoever.
Solution to stop this:
Add a boolean Extra to the intent created by the Receiver
serviceIntent.putExtra("isFromReceiver", true);
context.startService(serviceIntent);
Then check for this boolean Extra while in onStart(Intent) of the Service
boolean isFromReceiver = intent.getBooleanExtra("isFromReceiver", false);
if( !isFromReceiver ) {
return; //just stop starting the service
}