Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我无故打开活动时,服务开始_Java_Android_Android Service - Fatal编程技术网

Java 当我无故打开活动时,服务开始

Java 当我无故打开活动时,服务开始,java,android,android-service,Java,Android,Android Service,我正在使用以下代码: public class ActivityMain extends Activity { private static final String TAG = "MainActivity"; private ServiceSpeechRecognition service; private ServiceConnection mConnection = new ServiceConnection() { public void onS

我正在使用以下代码:

public class ActivityMain extends Activity {
    private static final String TAG = "MainActivity";

    private ServiceSpeechRecognition service;
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder binder) {
            ServiceSpeechRecognition.MyBinder b = (ServiceSpeechRecognition.MyBinder) binder;
            service = b.getService();

            Log.e(TAG, "Service connected");
        }

        public void onServiceDisconnected(ComponentName className) {
            savePrefs();

            service = null;

            Log.e(TAG, "Service disconnected");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e(TAG, "onCreate");

        setContentView(R.layout.activity_ui_main);

        getPackageManager().setComponentEnabledSetting(new ComponentName(this, getPackageName() + ".MainActivity-Alias"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Log.e(TAG, "onResume");
        GoogleSearchApi.registerQueryGroup(this, ReceiverGoogleSearch.group);

        // Load prefs
        SharedPreferences prefs = ActivityMain.getPrefs(this);
        String key_phrase = prefs.getString(Preferences.KEY_PHRASE_KEY, Preferences.DEFAULT_KEY_PHRASE);
        boolean require_charge = prefs.getBoolean(Preferences.KEY_REQUIRE_CHARGER, true);

        // Update Ui
        EditText text = (EditText) findViewById(R.id.key_phrase);
        text.setText(key_phrase);

        CheckBox checkbox = (CheckBox) findViewById(R.id.require_battery);
        checkbox.setChecked(require_charge);

        // If should, start intent
        if (!require_charge || Util.isCharging(this)) {
            bindIntent();
        }
    }

    @Override
    protected void onPause() {        
        if (service != null) {
            Log.e(TAG, "Unbind");

            unbindService(mConnection);
        }

        super.onPause();
    }

    private void bindIntent() {
        Log.e(TAG, "Bind intent");

        Intent intent = new Intent(this, ServiceSpeechRecognition.class);
        startService(intent);
        bindService(intent, mConnection, 0);
    }

    public void setKeyPhrase(View view) {
        savePrefs();

        if (service != null) {
            EditText text = (EditText) findViewById(R.id.key_phrase);
            String key_phrase = text.getText().toString();

            service.setKeyPhrase(key_phrase);
        }

        Toast.makeText(this, R.string.str_key_phrase_updated, Toast.LENGTH_SHORT).show();
    }

    public void setRequireCharge(View view) {
        savePrefs();

        CheckBox checkbox = (CheckBox) view;
        boolean require_charge = checkbox.isChecked();

        if (service != null) {
            service.setRequiresCharge(require_charge);
        } else if (!require_charge || Util.isCharging(this)) {
            bindIntent();
        }
    }

    public void savePrefs() {
        EditText text = (EditText) findViewById(R.id.key_phrase);
        String key_phrase = text.getText().toString();

        CheckBox checkbox = (CheckBox) findViewById(R.id.require_battery);
        boolean require_charge = checkbox.isChecked();

        SharedPreferences.Editor prefs = ActivityMain.getPrefs(this).edit();

        prefs.putString(Preferences.KEY_PHRASE_KEY, key_phrase);
        prefs.putBoolean(Preferences.KEY_REQUIRE_CHARGER, require_charge);

        prefs.commit();
    }

    public static SharedPreferences getPrefs(Context context) {
        return context.getSharedPreferences(Preferences.KEY, Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);   
    }
}

这是一项启动服务的活动。。问题在于,服务一打开活动就启动。我会创建一个按钮或复选框来启动它。但现在,当我打开活动时,它会启动服务。我没有在onCreate()方法中编写任何
startService()

在这里,您可以在Resume中启动您的服务,因此请替换

private void bindIntent() {
        Log.e(TAG, "Bind intent");

        Intent intent = new Intent(this, ServiceSpeechRecognition.class);
        startService(intent);
        bindService(intent, mConnection, 0);
    }

在按钮单击事件中输入以下代码

Intent intent = new Intent(this, ServiceSpeechRecognition.class);
startService(intent);

就是这样……

你能把标题改成比我不想启动服务时更有意义的标题吗?我想你的bindIntent函数正在onResume中调用,请先确认方法
bindIntent()
onResume()
中被调用,这就是启动你的
服务的原因。谢谢。但通过这种方式,如果我单击复选框,它将启动服务,但不会停止。在服务类中创建方法StopService,并在需要停止服务时调用它。就是这样。
Intent intent = new Intent(this, ServiceSpeechRecognition.class);
startService(intent);