Android service does not start

advertisements

Ok so this is the problem. I do context.startService(serviceIntent), but i don't get new log message from OnCreate or OnStartCommand.

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.distorted.flashnotifier"
    android:versionCode="0"
    android:versionName="0.6" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.distorted.flashnotifier.Home"
            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="FlashService"
            android:exported="true"
            android:enabled="true"
            android:permission="android.permission.READ_PHONE_STATE">
        </service>
    </application>
</manifest>

What i already tried:

  • Changing service name to ".FlashService" and "com.distorted.flashnotifier.FlashService"

Activity code:

private Context cont;

protected void onCreate(Bundle savedInstanceState) {
    cont = getApplicationContext();
}

public void setNotificator() {
    Intent serviceIntent = new Intent(cont, FlashService.class);
    if(callNotificator){
        cont.startService(serviceIntent);
        Log.d("LOG", "setNotificator works");
    } else {
        cont.stopService(serviceIntent);
    }
}

What have i tried:

  • changing all "cont" to "this"
  • startService(new Intent(cont, FlashService.class));

FlashService.class:

package com.distorted.flashnotifier;

public class FlashService extends Service{

    public void OnCreate () {
        Log.d("LOG", "onCreate works");
    }

        public int OnStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

I tried:

  • moving Log.d to onStartCommand
  • changed START_STICKY to super.onStartCommand(intent, flags, startId)

Ok so I get that setNotificator log message, but not the onCreate one. Does anyone know how could I make this work? (Thanks for reading the whole thing). LogCat doesn't say anything interesting.

UPDATE:

full Activity code:

package com.distorted.flashnotifier;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;

public class Home extends Activity {

    private Button timeSelectBut1;
    private Button timeSelectBut2;
    private int hour1;
    private int hour2;
    private int minute1;
    private int minute2;
    static final int TIME_DIALOG_ID1 = 1;
    static final int TIME_DIALOG_ID2 = 2;
    private Switch callSwitch;
    private Switch notificationSwitch;
    private boolean callNotificator;
    private boolean notifNotificator;
    private Context cont;

//IMPORTANT UNTIL timeSelectBut1.setOnClickListener

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
            cont = getApplicationContext();
        timeSelectBut1 = (Button) findViewById(R.id.selectTimeButton1);
        timeSelectBut1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID1);
            }
        });
        final Calendar cal1 = Calendar.getInstance();
        hour1 = cal1.get(Calendar.HOUR_OF_DAY);
        minute1 = cal1.get(Calendar.MINUTE);
        updateText(1);

        timeSelectBut2 = (Button) findViewById(R.id.selectTimeButton2);
        timeSelectBut2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    showDialog(TIME_DIALOG_ID2);
                }
            });
            final Calendar cal2 = Calendar.getInstance();
            hour2 = cal2.get(Calendar.HOUR_OF_DAY);
            minute2 = cal2.get(Calendar.MINUTE);
            updateText(2);
    }

    @Override
    protected Dialog onCreateDialog(int id){
        if (id == 1) {
            return new TimePickerDialog(this, mTimeSetListener1, hour1, minute1, true);
        }
        if (id == 2) {
            return new TimePickerDialog(this, mTimeSetListener2, hour2, minute2, true);
        }
        return null;
    }

    public void updateText(int id) {
        if (id == 1) timeSelectBut1.setText(pad(hour1) + ":" + pad(minute1));
        if (id == 2) timeSelectBut2.setText(pad(hour2) + ":" + pad(minute2));
    }

    private TimePickerDialog.OnTimeSetListener mTimeSetListener1 = new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    hour1 = hourOfDay;
                    minute1 = minute;
                    updateText(1);
                }
            };
    private TimePickerDialog.OnTimeSetListener mTimeSetListener2 = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            hour2 = hourOfDay;
            minute2 = minute;
            updateText(2);
        }
    };

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

//IMPORTANT

    public void onCallSwitchClicked(View view) {
        boolean on = ((Switch) view).isChecked();
        if (on) {
            callNotificator = true;
            setNotificator();
            //timeSelectBut1.setText("callNotifTrue");
        } else {
            callNotificator = false;
            setNotificator();
            //timeSelectBut1.setText("callNotifFalse");
        }
    }

    public void onNotifSwitchClicked(View view) {
        boolean on = ((Switch) view).isChecked();
        if (on) {
            notifNotificator = true;
            //setNotificator();
        } else {
            notifNotificator = false;
            //setNotificator();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

//IMPORTANT

    public void setNotificator() {
        Intent serviceIntent = new Intent(this, FlashService.class);
        if(callNotificator){
            startService(serviceIntent);
            Log.d("LOG", "SetNotificator works");
        } else {
            stopService(serviceIntent);
        }
    }
}


You did not register the service properly. Delete the line android:permission in service Replace your manifest by this.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.distorted.flashnotifier"
android:versionCode="0"
android:versionName="0.6" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.distorted.flashnotifier.Home"
        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=".FlashService"
        android:exported="true"
        android:enabled="true"
       >
    </service>
</application>
</manifest>

EDIT:

After trying your code I realized that, you did not override oncreate and onstartcommand properly. In Names of the method onCreate() and onStartCommand() , o is not caps.. Please change it to small o and try.