I am using toggleButton in my code for start/stop service. I start service using togglebutton. but when I reboot my phone I want the toggleButton to be on i.e service is started as soon as phone is on. This is my code.
public void onCreate(Bundle savedInstanceState) {
Toast.makeText(getApplicationContext(),"in OnCreate", Toast.LENGTH_LONG).show();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// setAppInfo();
dba = new DBAdapter(this);
//toggleLogging(AppSettings.getServiceRunning(MainActivity.this),
//AppSettings.getLoggingInterval(MainActivity.this));
addButtonListeners();
enableControls();
try {
SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0);
String installDate = preferences.getString("FirstRun", null);
if (installDate == null) {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imeino = tm.getDeviceId();
String model = getDeviceName();
SmsManager smsManager = SmsManager.getDefault();
SharedPreferences.Editor editor = preferences.edit();
sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = sdf.format(new Date(System
.currentTimeMillis()));
editor.putString("FirstRun", dateString);
editor.commit();
} else {
}
}// end of try
catch (Exception e2) {
e2.printStackTrace();
}
}// -----------------end of create method---
private Runnable mUpdate = new Runnable() {
@Override
public void run() {
try {
// TestFunction();
//checkDatabase();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mHandler.postDelayed(this, 60000);
}
};
public void onBackPressed()
{
moveTaskToBack (true);
}
private void addButtonListeners()
{
Toast.makeText(getApplicationContext(),"in addButtonListener", Toast.LENGTH_LONG).show();
((Button) findViewById(R.id.start_logging)).setOnClickListener(btnClick);
//((Button) findViewById(R.id.exit)).setOnClickListener(btnClick);
}
private void toggleLogging(boolean isStart, int interval)
{
Toast.makeText(getApplicationContext(),"in toggleLogging", Toast.LENGTH_LONG).show();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent loggerIntent = PendingIntent.getBroadcast(this, 0,new Intent(this, AlarmReceiver.class), 0);
Toast.makeText(getApplicationContext(),"in toggleLogging2", Toast.LENGTH_LONG).show();
if (isStart)
{
Toast.makeText(getApplicationContext(),"is Start", Toast.LENGTH_LONG).show();
manager.cancel(loggerIntent);
AppSettings.setServiceRunning(this, false);
AppLog.logString("Service Stopped.");
}
else
{
Toast.makeText(getApplicationContext(),"in stop", Toast.LENGTH_LONG).show();
setLogFileName();
long duration = interval * 60 * 1000;
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), duration, loggerIntent);
AppSettings.setServiceRunning(this, true);
AppLog.logString("Service Started with interval " + interval+ ", Logfile name: " + AppSettings.getLogFileName(this));
AppLog.logString("Service Started with interval " + interval+ ", Logfile name: " + AppSettings.getLogFileName1(this));
}
Toast.makeText(getApplicationContext(),"in toggleLogging3", Toast.LENGTH_LONG).show();
}
private void enableControls()
{
Toast.makeText(getApplicationContext(),"in enableControls", Toast.LENGTH_LONG).show();
boolean isServiceRunning = AppSettings.getServiceRunning(this);
String buttonText = getString(R.string.start_logging);
if (isServiceRunning)
{
buttonText = getString(R.string.stop_logging);
// ((Button)findViewById(R.id.logging_interval)).setEnabled(false);
}
else
{
// ((Button)findViewById(R.id.logging_interval)).setEnabled(true);
}
((Button) findViewById(R.id.start_logging)).setText(buttonText);
}
private void changeLoggingIntercal()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String loggingIntervals[] = { "1 minutes", "5 minutes","30 minutes", "1 hour" };
builder.setTitle(getString(R.string.logging_interval));
builder.setSingleChoiceItems(loggingIntervals, currentIntervalChoice,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
currentIntervalChoice = which;
setLoggingInterval(currentIntervalChoice);
dialog.dismiss();
}
});
builder.show();
}
private void setLoggingInterval(int intervalChoice)
{
Toast.makeText(getApplicationContext(),"in setLoggingInterval", Toast.LENGTH_LONG).show();
int interval = 3;
switch (intervalChoice)
{
case 0:
interval = 1;
break;
case 1:
interval = 5;
break;
case 2:
interval = 30;
break;
case 3:
interval = 60;
break;
default:
interval = 1;
break;
}
AppSettings.setLoggingInterval(this, interval);
}
public void setLogFileName()
{
String filename = "gpsdata.txt";
String fn="gpsdata1.txt";
AppSettings.setLogFileName(this, filename);
AppSettings.setLogFileName1(this, fn);
}
private View.OnClickListener btnClick = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.start_logging:
{
toggleLogging(AppSettings.getServiceRunning(MainActivity.this),
AppSettings.getLoggingInterval(MainActivity.this));
enableControls();
break;
}
}
}
};
the application starts but service is not restarted.