0

我有一个简单的活动,可以启动和停止记录 GPS 数据的后台服务。然后我通过单击主页按钮将该过程置于后台。

当我通过单击它的图标返回应用程序时,它会创建一个新进程而不是恢复到原始活动。如果我然后关闭此过程(通过单击后退按钮),它会调用 onDestroy() 然后恢复到我原来的活动屏幕,显示该服务仍在录制。这对于应用程序的预期用途非常不便。

我的理解是应该从活动堆栈中弹出原始活动,而不是创建一个新进程。

下面是我的活动代码。我将不胜感激任何人对此的解释。

public class MainActivity extends Activity {

    GPSTracker gps;
    private static final String TAG = "MEDIA";
    private String tv;
    private boolean record = false;
    private   TextView status = null;

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

        if (savedInstanceState != null) 
            record = savedInstanceState.getBoolean("Status");
        Toast.makeText(getApplicationContext(),"onCreate",Toast.LENGTH_LONG).show();

        setContentView(R.layout.activity_main);
    }

    public void StartRecord(View v) {
        Button btn = (Button) findViewById(R.id.record);
        status = (TextView) findViewById(R.id.status);

        if (record == false) {

            btn.setText("Stop Record");
            status.setText("... Recording ...");

            record = true;

            ComponentName comp = new ComponentName(getPackageName(), LogService.class.getName());
            ComponentName service = startService(new Intent().setComponent(comp));
        }
        else {
            record = false;
            status.setText("... Standing By ...");
            stopService(new Intent(MainActivity.this,LogService.class));
            btn.setText("Start Record");
        }

    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Toast.makeText(getApplicationContext(), "onSaveInstanceState",Toast.LENGTH_LONG).show();
        savedInstanceState.putBoolean("Status", record);
        // etc.
    }
    @Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first
        Button btn = (Button) findViewById(R.id.record);
        Toast.makeText(getApplicationContext(), "onResume",Toast.LENGTH_LONG).show();
        if (record == true) 
            btn.setText("Stop Record");
        else

            btn.setText("Start Record");

    }

    @Override
    public void onStart() {
        super.onStart();  // Always call the superclass method first
        Toast.makeText(getApplicationContext(), "onStart",Toast.LENGTH_LONG).show();

    }

    @Override
    public void onRestart() {
        super.onRestart();  // Always call the superclass method first
        Toast.makeText(getApplicationContext(), "onRestart",Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();  // Always call the superclass method first
        Toast.makeText(getApplicationContext(), "onDestroy",Toast.LENGTH_LONG).show();

    }
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
        Toast.makeText(getApplicationContext(), "onRestoreInstanceState",Toast.LENGTH_LONG).show();
        record = savedInstanceState.getBoolean("Status");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
4

1 回答 1

0

试试这个并覆盖下面的功能,它应该在你的情况下工作正常。

onResume() -> 设置记录=真

onPause() -> 设置记录=假

于 2013-04-10T03:58:59.030 回答