How to start service at boot on android 4.0.3 - android

I wan't to start service at boot on android 4.0.3 but when i boot my device it not happen in my device and service not start.Help me please thank you
here is my manifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.android.testsharedpreference.MyService"></service>
<receiver android:name="com.android.testsharedpreference.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
my boardcast receiver
public class BootReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
context.startService(new Intent(context,MyService.class));
}
}
}
and my service
public class MyService extends Service
{
#Override
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onCreate()
{
super.onCreate();
Toast.makeText(MyService.this, "onCreate Service ", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(MyService.this, "onDestroy Service ", Toast.LENGTH_LONG).show();
}
}

you should add add android.permission.RECEIVE_BOOT_COMPLETED as it was not required before ICS so the app was working fine but starting from ICS is you don't have this permission your app wont receive the boot completed intent

Don't forget to override the method "onStartCommand" in your MyService:
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String action = intent == null ? null : intent.getAction();
Log.d("onStartCommand", "action = " + action);
/* ... */
return START_NOT_STICKY; // for example ...
}

Related

Android self startup

I wrote a application,I wrote an application. I want it to start a service to connect to the WebSocket server, and then send a notification at an appropriate time,I tried to write a piece of code, but the server log has no connection information,This is my
code:
In mainifests.xml:
<!--permission-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!--receiver -->
<receiver android:name=".service.StartNotificationReceiver"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
<!--service-->
<service android:name=".service.NotificationService"/>
In NotificationService.java:
package com.xxxx.xxxx.service;
public class NotificationService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
}
private void createChannel(){
}
private void sendNotification(String title,String content){
}
}
In StartNotificationReceiver.java
package com.ecsoft.zyymaintain.service;
public class StartNotificationReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
Log.e(“sot”,”onReceive:"+intent.getAction());
if (ACTION.equals(intent.getAction())){
Intent intentService = new Intent(context, NotificationService.class);
context.startService(intentService);
}
}
}
This is my core code,and my application is cant start service when OS startup
I use below code can start the service
<service
android:name=".AutoStartService"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".AutoStartReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

How to make an application start on factory reset/first startup?

I am looking to have some basic forms run on my android device after it is reset or on the first boot like most smartphones ask you to connect to Wi-Fi, register the device etc. I haven't worked with native android development previously so what are my options and what areas should I look into?
Try this
BootCompleteReceiver.java
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MsgPushService.class);
context.startService(service);
}
}
MyService.java
public class MyService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
MainActivity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(getBaseContext(), MyService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbootservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name=".MyService"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Android - Restrict to launch

I found really interesting option in Lenovo K5
When this option is active, my background services doesn't activated after restart of the device. I searched everywhere in the web, and I set some tweaks in the manifest, but nothing helps.
I notice that in some apps, that option is not activated, like Facebook or Viber. So when they can made it, I'm sure that it have some workaround of the problem.
Thanks in advance.
MyService.java
public class MyService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startid) {
// some logic
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
}
}
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// some logic
}
}
MyAutoStart.java
public class MyAutoStart extends BroadcastReceiver {
/**
* Listens for Android's BOOT_COMPLETED broadcast and then executes
* the onReceive() method.
*/
#Override
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, ServiceMaps.class);
context.startService(intent);
}
}
Manifest.xml
<receiver android:name=".MyReceiver" />
<receiver
android:name=".MyAutoStart"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name=".MyService" />

Unable to create service

I'm new to android and creating a service in one of my reminder application. but I my service is triggering when i call the startService() in my onCreate() method of my activity I am unable to see the toasts which are used in different methods of my service class.As new to android unable to figure out the problem.
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_show_reminders);
isReminders = false;
dataSrc = new RemitDataSrc(this);
dataSrc.open();
rem = dataSrc.findALL();
refreshDisplay();
}
private void refreshDisplay(){
if(isMyServiceRunning(GeofencingService.class)){
Intent intent = new Intent(this, GeofencingService.class);
stopService(intent);
Toast.makeText(this, "yes service", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "No service", Toast.LENGTH_LONG).show();
}
if(rem.size()==0){
Toast.makeText(this, "No Reminders in Database", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent(this, GeofencingService.class);
startService(intent);
ArrayAdapter<Reminders> remAdpt = new RemindersListAdapter(this, rem);
setListAdapter(remAdpt);
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
GeofencingService:
public class GeofencingService extends Service{
RemitDataSrc dataSrc;
/**
* Geofence Store
*/
private GeofenceStore mGeofenceStore;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "GeoFencing Service created", Toast.LENGTH_LONG).show();
dataSrc = new RemitDataSrc(this);
dataSrc.open();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "GeoFencing Service Started", Toast.LENGTH_LONG).show();
serviceTest sT= new serviceTest();
dataSrc.open();
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "GeoFencing Service Stoped", Toast.LENGTH_LONG).show();
dataSrc.close();
}
}
manifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SetReminderActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyD5GMap3cZBiMYWEiK0WX-2whx0xuqBwW4" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".AddReminderActivity"
android:label="#string/title_activity_add_reminder" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityShowReminders"
android:label="#string/title_activity_activity_show_reminders" >
</activity>
<activity
android:name=".RemindersDetailActivity"
android:label="Reminder Details" >
</activity>
<service
android:name=".LocationCheckerService"
>
</service>
<service
android:name=".GeofencingService"
android:exported="false"
>
</service>
</application>
As you have mentioned in one of one of your comments that the package name of service is different from the package containing activity, use the fully qualified class name in manifest with package name.
<service
android:name="com.afifa.projectreminder.service.GeofencingService" android:exported="false"
>
</service>
Please register your service to your manifest as a children of your application like:
<application ..>
..
<service
android:name=".GeofencingService"
android:enabled="true">
</service>
</application>
Mention the service in the manifest of your project under application Tag.
like so
<service android:name=".ServiceName" />

Android-Boot Completed is not working in Broadcastreceiver

I'm using android(version 4.1.1) MeLE box(SmartTv) for developing one application, i need to start up my application when device Boot Time is completed but my device not catch up the BOOT_COMPLETED Action. If i'm use that same application in mobiles or emulator the Boot_Completion action were caught by Broadcast_receiver.
if anyone known about this issue help me thanks in advance....
here is my code...
manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="NotifyingDailyService" >
</service>
BootCompletedReceiver class:
public class BootCompletedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.w("boot_broadcast_poc", "starting service...");
context.startService(new Intent(context, NotifyingDailyService.class));
}
}
Service class:
public class NotifyingDailyService extends Service {
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");
return super.onStartCommand(intent, flags, startId);
}
}
One thing I noticed is you don't have the category set for your receiver in your Manifest. The following works for me in my App.
<receiver android:name="us.nineworlds.serenity.StartupBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Then in my StartupBroadcastReceiver, i have the following
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) {
return;
}
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean startupAfterBoot = prefs.getBoolean("serenity_boot_startup", false);
if (startupAfterBoot) {
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
This will start the MainActivity class once bootup has been completed.
Link to the project code is here: https://github.com/NineWorlds/serenity-android/blob/master/serenity-app/src/main/java/us/nineworlds/serenity/StartupBroadcastReceiver.java

Categories

Resources