【问题标题】:How to stop an intent activity from a service如何停止服务的意图活动
【发布时间】:2015-09-30 09:23:33
【问题描述】:

我已经从服务启动了一个 Intent 活动。现在,我怎样才能阻止服务本身的意图活动?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startService (View view) {
        Intent intent = new Intent(this,MainService.class);
        startService(intent);
    }

    public void stopService (View view) {
        Intent intent = new Intent(this,MainService.class);
        stopService(intent);

    }
}

startService、stopService 有关联的按钮。

public class MainService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service started",Toast.LENGTH_LONG).show();
        Intent dialogIntent = new Intent(this, calledActivity.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(dialogIntent);

        return START_STICKY;
        //return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        //super.onDestroy();
        Toast.makeText(this, "Service stopped",Toast.LENGTH_LONG).show();

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //Intent dialogIntent = new Intent(this, MainActivity.class);
        //dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //startActivity(dialogIntent);
        return null;
    }

}


public class calledActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "I am here", Toast.LENGTH_LONG).show();
        displayNumber();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Toast.makeText(this, "I am stopping", Toast.LENGTH_LONG).show();
        finish();
    }

    @Override
    protected void onDestroy () {
        super.onDestroy();
        Toast.makeText(this, "I am being destroyed", Toast.LENGTH_LONG).show();
        finish();
    }

    public void displayNumber () {
        TextView tv = (TextView)findViewById(R.id.textView);
        Integer counter = 10;
        while (counter > 0) {
            //tv.setText("Here you go" + String.valueOf(counter));
            tv.setText("Here you go" + counter);
            counter = counter - 1 ;
            Toast.makeText(this, "I am there "+counter, Toast.LENGTH_LONG).show();

        }
    }


}

当服务调用意图活动时,我认为意图活动处于前台。现在,每当我按下停止服务按钮时,应用程序就会崩溃。我相信 stopService 与服务类相关联,但与意图活动无关。有什么方法可以阻止服务的意图活动?

【问题讨论】:

  • 发布您按下按钮并崩溃的代码。同时发布来自 logcat 的堆栈跟踪。

标签: android android-intent android-activity android-service


【解决方案1】:

Service 在UI 线程上工作。尝试使用IntentService。它在后台线程上运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多