如何使我的应用程序在后台运行?

时间:2019-02-10 04:09:24

标签: android android-studio

我的应用程序都是关于紧急短信的,只需单击音量按钮,然后将模拟的消息发送到某个电话号码。

我想知道如何在需要时使应用程序在后台运行,并在需要时关闭它?

希望得到您的帮助,我的代码是这样的:

public class MainActivity extends Activity {

    private final static int SEND_SMS_PERMISSION_REQUEST_CODE = 111;
    private Button sendMessage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendMessage = findViewById(R.id.send_message);
        final EditText phone = findViewById(R.id.phone_no);
        final EditText message = findViewById(R.id.message);
        sendMessage.setEnabled(false);


        if (checkPermission(Manifest.permission.SEND_SMS)) {
            sendMessage.setEnabled(true);
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQUEST_CODE);
        }

        sendMessage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String msg = message.getText().toString();
                String phonenumber = phone.getText().toString();

                if (!TextUtils.isEmpty(msg) && !TextUtils.isEmpty(phonenumber)) {

                    if (checkPermission(Manifest.permission.SEND_SMS)) {
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(String.valueOf(phone), null, msg, null, null);
                    } else {
                        Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Enter a message and a phone number", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
            final EditText phone = findViewById(R.id.phone_no);
            final EditText message = findViewById(R.id.message);
            String msg = message.getText().toString();
            String phonenumber = phone.getText().toString();

            if (!TextUtils.isEmpty(msg) && !TextUtils.isEmpty(phonenumber)) {

                if (checkPermission(Manifest.permission.SEND_SMS)) {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(String.valueOf(phone), null, msg, null, null);
                } else {
                    Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(MainActivity.this, "Enter a message and a phone number", Toast.LENGTH_SHORT).show();
            }
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }

    private boolean checkPermission(String permission) {
        int checkPermission = ContextCompat.checkSelfPermission(this, permission);
        return checkPermission == PackageManager.PERMISSION_GRANTED;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case SEND_SMS_PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    sendMessage.setEnabled(true);
                }

                break;
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

You have to start a service in your Application class to run it always. If you do that, your service will be always running.Even though user terminates your app from task manager or force stop your app, it will start running again in Background.

You can get more info about service from this link:
https://developer.android.com/training/run-background-service/create-service