это есть в манифесте
Код: Выделить всё
Код: Выделить всё
public class MyBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean auto = prefs.getBoolean("as", true);
if(auto == true) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Код: Выделить всё
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
autostart= (Switch) findViewById(R.id.switch1);
autostart.setOnClickListener(this);
settings = getSharedPreferences("as", MODE_PRIVATE);
editor = settings.edit();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean auto = prefs.getBoolean("as", false);
if(auto == true){
autostart.setChecked(true);
}else{
autostart.setChecked(false);
}
@Override
public void onClick(View view) {
Switch auto = (Switch) view;
if (auto.isChecked())
{
editor.putBoolean("as",true);
Toast.makeText(SettingsActivity.this, "autostart on", Toast.LENGTH_SHORT).show();
editor.commit();
}else{
editor.putBoolean("as",false);
Toast.makeText(SettingsActivity.this, "autostart off", Toast.LENGTH_SHORT).show();
editor.commit();
}
}
Подробнее здесь: https://stackoverflow.com/questions/354 ... rt-android
Мобильная версия