Reputation: 398
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:
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:
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:
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);
}
}
}
Upvotes: 2
Views: 5121
Reputation: 6980
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.
Upvotes: 0
Reputation: 11254
Since you are starting your Service
from Activity
, the correct code is:
public void setNotificator() {
Intent serviceIntent = new Intent(this, FlashService.class);
if(callNotificator){
startService(serviceIntent);
Log.d("LOG", "setNotificator works");
} else {
stopService(serviceIntent);
}
}
Moving forward, per Docs: The system calls this method when the service is first created, to perform one-time setup procedures (before it calls either onStartCommand()
or onBind()
). If the service is already running, this method is not called.
So I'll better put Log
call to the onStartCommand
And your service is using permission, again from Docs: If a caller of startService()
, bindService()
, or stopService()
, has not been granted this permission, the method will not work and the Intent object will not be delivered to the service. If this attribute is not set, the permission set by the <application>
element's permission attribute applies to the service. If neither attribute is set, the service is not protected by a permission.
Upvotes: 0