Question

I am trying to create an Android app in Android Studio. The app is mainly a background service to send some information to the server around every 15 seconds or so. I have created an empty activity, boot receiver and a service class, but it doesn't work. Could somebody help me, and explain why it doesn't work.

The relevant files:

MainActivity.java

package nl.robinvandervliet.robinsapp.app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        finish();
    }
}

BootReceiver.java

package nl.robinvandervliet.robinsapp.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        context.startService(new Intent(context, RobinsService.class));
    }
}

RobinsService.java

package nl.robinvandervliet.robinsapp.app;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class RobinsService extends Service
{
    private Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {
            //Opening connection to the server.

            while (true)
            {
                //Sending some data to the server.
                //Sleeping for around 15 seconds.
            }

            //Dead code, should never reach this place.
        }
    };

    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        new Thread(runnable).start();

        Notification notification = new Notification.Builder(getApplicationContext()).setContentTitle("MyService is running!").setContentTitle("(more information later)").build();
        startForeground(1, notification);
        Toast.makeText(getApplicationContext(), "Service created!", Toast.LENGTH_LONG).show();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nl.robinvandervliet.robinsapp.app" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="nl.robinvandervliet.robinsapp.app.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>

        <receiver android:name="nl.robinvandervliet.robinsapp.app.BootReceiver" android:enabled="true" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name="nl.robinvandervliet.robinsapp.app.RobinsService" android:exported="false" />
    </application>

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

</manifest>

Thanks, Robin.

No correct solution

OTHER TIPS

Since you haven't implemented some parts yet I assume you just want it to run when you boot your device. Maybe take a look here. Adding an extra action in the manifest might solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top