1

I trying to write simple basic application that will start my service when the phone is start.

i add all the permission i need.

I install the application on my android ( android 6.01 ). And when i reboot my phone - i can't see that the service is up or did any action.

  1. why this is not working ?
  2. how can i debug a service on android ?

The code:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAG" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">

    <service android:name=".MyService" android:enabled="true" android:exported="true"/>

    <receiver android:name=".MainBroadcastReceiver" android:enabled="true" android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
</application>



public class MainBroadcastReceiver extends BroadcastReceiver {
    public MainBroadcastReceiver(){

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, MyService.class));
    }
}




public class MyService extends Service {

private File _file;
private Timer _timer;
private FileOutputStream _fileOutputStream;

public MyService() throws IOException {

    _file = new File("/sdcard/" + "myServiceText.txt");
    _file.createNewFile();

    _fileOutputStream = openFileOutput(_file.getName(), Context.MODE_APPEND);
    _fileOutputStream.write("Ctor called".getBytes());
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}


@Override
public void onCreate() {
    super.onCreate();

    _timer = new Timer("timer");
    _timer.schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            try {
                _fileOutputStream.write("onCreate Called".getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, 0, 5000);

}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        _fileOutputStream.write("onStartCommand".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return START_STICKY;
}
Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • Have you run it once on the device first? Things that run at boot have to be run manually by the user at least once before they are allowed to automatically start. – alzee Aug 06 '16 at 16:10
  • yes, i run it on my device more then once. i turn on all the permission manually also – Yanshof Aug 06 '16 at 16:11
  • Ah.. you're missing category in your XML. Add `` to your intent filter and try again. – alzee Aug 06 '16 at 16:15
  • Code looks fine to me. However, your app wii not receive the `BOOT_COMPLETE` if it is force-stopped or crashed. Are you sure you don't fall in either categories? You can show a `Toast` from your `Service` to ensure it is running. – Shaishav Aug 06 '16 at 16:16
  • adding the 'category android:name="android.intent.category.DEFAULT" not helping ... still same – Yanshof Aug 06 '16 at 16:24
  • 1
    I don't see an a `Activity` listed in your manifest. How exactly are you running your app before rebooting? – Mike M. Aug 06 '16 at 17:36
  • Sorry, but i did know that i need Activity when i just want to create a service that run on startup without any UI. How do i make the ui be disable and not shown ? ? ? – Yanshof Aug 06 '16 at 18:04
  • 1
    You don't need to show the `Activity` every time. The user just needs to run your app at least once after installation to bring it out of the _stopped_ state. This is usually done by the user starting its main `Activity`. Otherwise, it will take another app explicitly launching a component in your app. – Mike M. Aug 06 '16 at 18:29

1 Answers1

1

Your code is fine, the reason your service is not running it's because your app is in Stopped State.

1. Stopped state is a security procedure that allows your app to "wake itself up" only after user first manually launches it from the home screen.(Unless you are a system)
By doing so android prevents malicious apps to be installed and run in your phone background without you knowing.

2. How to debug a background process?
When working with background processes there are two great tools you need in your belt -
First is the adb broadcast command -
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

Second is attach remote debugger (Android Studio):
Run | Attach debugger to android process (last option in the menu) | choose your app's process.

Community
  • 1
  • 1
Nir Duan
  • 6,164
  • 4
  • 24
  • 38
  • Thanks, I checked all the attached link .But i still can't understand .. do i need add an Activity ? Is there any code example that you can show because i can't find any example on google – Yanshof Aug 07 '16 at 04:23
  • 1
    Yes you need to add at least one activity and launch it manually via the phone home screen. – Nir Duan Aug 07 '16 at 05:57