如何完成活动不启动服务

时间:2016-04-30 10:58:24

标签: android

如果没有按下按钮,我想开始服务,如果它已经离开我的活动,我想要如果用户已锁定设备(屏幕关闭),那么也要离开活动。如果屏幕关闭但是它没有离开活动,我已经尝试了这个代码离开活动。为什么会发生这种情况?我应该怎么做才能解决它?

我的代码:

public class MyActivity extends Activity{
    boolean finishOrNot = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("Point", "yes1");
        if (getIntent().getBooleanExtra("Exit",false)==true){
           finishOrNot = true;
            Log.i("Point","yes");
        } else if(getIntent().getBooleanExtra("Exit",false)==false){
            Log.i("Point","No");
        }

        //finishOrNot = true;
        Log.i("Point", "User started app");
        final EditText editText1 = (EditText) findViewById(R.id.editText);
        ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startService(new Intent(MainActivity.this, MyService.class));
                String phoneNumber = editText1.getText().toString();
                Log.i("Number", phoneNumber);
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + phoneNumber));
                startActivity(callIntent);
                finishOrNot = true;
            }
        });

    }

    @Override
    protected void onUserLeaveHint() {
        if (finishOrNot==false){
            finish();
            Log.i("Point","finishOrNot == false");
        } else{Log.i("Point","finishOrNot == true");}
        super.onUserLeaveHint();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //startActivity(new Intent(MyActivity.this,MyActivty.class));
        Log.i("Point", "MyActivity onDestroy");
        if (finishOrNot == false) {
            startService(new Intent(MyActivity.this, MyService.class));
        }else if(finishOrNot==true) {
            Log.i("Point","fon=true");
        }
    }
}

我的logcat:

04-30 05:53:30.190  14030-14030/com.example.amadey.myapplication5 I/Point﹕ yes1
04-30 05:53:30.190  14030-14030/com.example.amadey.myapplication5 I/Point﹕ No
04-30 05:53:30.200  14030-14030/com.example.amadey.myapplication5 I/Point﹕ User started app
04-30 05:54:17.560  14030-14030/com.example.amadey.myapplication5 I/Point﹕ EndReceiver
04-30 05:54:18.300  14030-14030/com.example.amadey.myapplication5 I/Point﹕ finishOrNot == false1
04-30 05:54:18.780  14030-14030/com.example.amadey.myapplication5 I/Point﹕ yes1
04-30 05:54:18.780  14030-14030/com.example.amadey.myapplication5 I/Point﹕ yes
04-30 05:54:18.990  14030-14030/com.example.amadey.myapplication5 I/Point﹕ User started app
04-30 05:54:19.410  14030-14030/com.example.amadey.myapplication5 I/Point﹕ finishOrNot == true1
04-30 05:54:20.750  14030-14030/com.example.amadey.myapplication5 I/Point﹕ MyActivty
04-30 05:56:38.920  14030-14030/com.example.amadey.myapplication5 I/Point﹕ finishOrNot == true1
04-30 05:56:46.120  14030-14030/com.example.amadey.myapplication5 I/Point﹕ MyActivity onDestroy
04-30 05:56:47.010  14030-14030/com.example.amadey.myapplication5 I/Point﹕ MyService

谢谢。

3 个答案:

答案 0 :(得分:1)

您可以通过电话完成活动:

finish();
Call this when your activity is done and should be closed.

答案 1 :(得分:0)

尝试此操作以在屏幕关闭模式下完成活动。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

 /**
 * Created by moorthy on 4/30/2016.
 */
public class SampleActivity extends AppCompatActivity {

private BroadcastReceiver mScreenStateReceiver = null;

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

    mScreenStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                Log.i("Check", "Screen went OFF");
                Toast.makeText(context, "screen OFF", Toast.LENGTH_LONG).show();
                //Here you can finish you activity
                finish();
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                Log.i("Check","Screen went ON");
                Toast.makeText(context, "screen ON",Toast.LENGTH_LONG).show();
            }
        }
    };

}


@Override
protected void onResume() {
    super.onResume();
    IntentFilter screenStateFilter = new IntentFilter();
    screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
    screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
    registerReceiver(mScreenStateReceiver, screenStateFilter);
}
}

答案 2 :(得分:0)

这也很好用

moveTaskToBack(true);
finish();

移动任务以支持其完成应用程序但仍在内存中。