android - 如何在现有应用程序上实现 Android 6.0 运行时权限

标签 android android-6.0-marshmallow runtime-permissions

问题:

我有一个现有的应用程序,我想在其上实现 Android 6.0 的运行时权限。我已经阅读了很多关于运行时权限的不同内容,但我似乎无法理解所有不同的 fragment 。我没有找到任何实际展示如何将其实现到现有 Activity 中的内容。

其他要点

当我运行针对 SDK v23 的现有应用程序时,我收到了预期的权限错误,但我收到的权限错误甚至不是我请求的权限。我在 list 文件中拥有 SEND_SMS 权限,但我收到的错误是针对 READ_SMS 的。我的应用程序在 6.0 之前的版本上运行良好,没有 READ_SMS。

我希望我的应用程序在启动后立即请求许可,因为该应用程序的唯一目的是发送短信,因此如果没有该许可,该应用程序就没有其他用途。

问题:

如何在应用启动后立即将 SEND_SMS 的运行时权限实现到我现有的 Activity 中?

这些权限的处理是否需要在后台线程中运行?

我是否还需要 READ_SMS 的权限,因为这是它给出的权限错误(即使我的应用程序从未使用过该权限)?

我现有的 Activity :

public class MainActivity extends Activity implements OnClickListener {

SimpleCursorAdapter mAdapter;
AutoCompleteTextView txtContract;
EditText txtTrip;
EditText txtDate;
Button btnSend;
Button btnUpdate;
String today;

String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

private static final String API_KEY = "abcxyz";
private static final String CONTRACT_REGEX = "^([a-zA-Z0-9_-]){5}$";
private static final String TRIP_REGEX = "^([a-zA-Z0-9_-]){1,10}$";
private static final String DATE_REGEX = "^\\d{2}\\/\\d{2}\\/\\d{4}$";
private static final String PHONE_NUMBER = "1234567890";
private static final String DATE_FORMAT = "MM/dd/yyyy";

private BroadcastReceiver sendBroadcastReceiver;
private BroadcastReceiver deliveryBroadcastReceiver;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // TODO - IMPLEMENT RUNTIME PERMISSIONS FOR ANDROID >= 6.0

    try {



        // Initialize Views
        txtContract = (AutoCompleteTextView) findViewById(R.id.txtContract);
        txtTrip = (EditText) findViewById(R.id.txtTrip);
        txtDate = (EditText) findViewById(R.id.txtDate);
        btnSend = (Button) findViewById(R.id.btnSend);
        btnUpdate = (Button) findViewById(R.id.btnUpdate);


        // Set Listeners
        txtDate.setOnClickListener(this);
        btnSend.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);


        // Set Date To Today And Format
        final Calendar td = Calendar.getInstance();
        int tYear = td.get(Calendar.YEAR);
        int tMonth = td.get(Calendar.MONTH);
        int tDay = td.get(Calendar.DAY_OF_MONTH);

        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
        td.set(tYear, tMonth, tDay);
        today = sdf.format(td.getTime());

        txtDate.setText(today);


        // Check If Device Is Capable Of Sending SMS
        PackageManager pm = this.getPackageManager();
        if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
                !pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {

            Toast.makeText(this, "Sorry, your device probably can't send SMS...",
                    Toast.LENGTH_SHORT).show();

        }

        // Send Receiver
        sendBroadcastReceiver = new BroadcastReceiver() {

            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Requesting trip...", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };


        // Delivery Receiver
        deliveryBroadcastReceiver = new BroadcastReceiver() {
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Trip request successful.", Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "Trip request failed.", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };


        // Register Receivers
        registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
        registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));


        // Set Up Adapter For Autocomplete
        initializeAutoCompleteAdapter();

    }
    catch (Exception ex) {

        Toast.makeText(this, "Error in MainActivity.onCreate: " + ex.getMessage(),
                Toast.LENGTH_SHORT).show();

    }
}

@Override
protected void onDestroy() {
    unregisterReceiver(sendBroadcastReceiver);
    unregisterReceiver(deliveryBroadcastReceiver);
    super.onDestroy();
}


// Auto Complete Adapter
public void initializeAutoCompleteAdapter() {

    // Set Database Handler
    final DBHelper DBHelper = new DBHelper(getBaseContext());

    // Set Up Adapter For Autocomplete (This does not run on the main UI thread)
    mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null,
            new String[] { "contract" },
            new int[] {android.R.id.text1},
            0);

    txtContract.setAdapter(mAdapter);

    mAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
        @Override
        public CharSequence convertToString(Cursor cursor) {

            final int colIndex = cursor.getColumnIndexOrThrow("contract");
            return cursor.getString(colIndex);

        }
    });

    mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence description) {

            String strContract = txtContract.getText().toString();
            return DBHelper.getContract(strContract);

        }
    });

}


// OnClickListener Handler
@Override
public void onClick(View v) {

    // Handle Clicked View
    switch (v.getId()) {

        // Date Field
        case R.id.txtDate:

            // Get Current Date
            final Calendar c = Calendar.getInstance();
            c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),0,0,0);
            int mYear = c.get(Calendar.YEAR);
            int mMonth = c.get(Calendar.MONTH);
            int mDay = c.get(Calendar.DAY_OF_MONTH);


            // Set Up DatePicker Dialog
            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int day) {

                            // Define A New Calendar For Formatting
                            final Calendar cf = Calendar.getInstance();

                            // Format Selected Date
                            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
                            cf.set(year,month,day);
                            String selectedDate = sdf.format(cf.getTime());

                            // Add Selected Date To EditText Field
                            txtDate.setText(selectedDate);
                        }
                    }, mYear, mMonth, mDay);


            // Set Max Date
            c.add(Calendar.DATE, 2);
            c.add(Calendar.SECOND, -1);
            datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());


            // Set Min Date
            c.add(Calendar.DAY_OF_MONTH,-5);
            c.add(Calendar.SECOND, 1);
            datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());


            // Display DatePicker
            datePickerDialog.show();

        break;

        // Submit Button
        case R.id.btnSend:

            Boolean rval = true;

            if (!Validation.isValid(txtContract, CONTRACT_REGEX, "Invalid Contract #", true)) rval = false;
            if (!Validation.isValid(txtTrip, TRIP_REGEX, "Invalid Trip #", true)) rval = false;
            if (!Validation.isValid(txtDate, DATE_REGEX, "Invalid Date", true)) rval = false;

            if(rval) {
                new ValidateAndSend(this).execute();
            }

        break;


        // Update Contract DB
        case R.id.btnUpdate:

            TelephonyManager tMgr = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
            String mPhoneNumber = tMgr.getLine1Number();
            new POSTAsync(this).execute(API_KEY, mPhoneNumber);

        break;

    }
}

// Validate And Send
class ValidateAndSend extends AsyncTask<String, String, Boolean>{

    private final WeakReference<MainActivity> MainActivityWeakRef;

    public ValidateAndSend(MainActivity mainActivity) {
        super();
        this.MainActivityWeakRef = new WeakReference<>(mainActivity);
    }

    // Define Variables
    String strContract = txtContract.getText().toString();
    String strTrip = txtTrip.getText().toString();
    String strDate = txtDate.getText().toString();
    String strMessage = strContract.concat("|").concat(strTrip).concat("|").concat(strDate);
    Boolean rval = true;


    @Override
    protected void onPreExecute() {
    }


    @Override
    protected Boolean doInBackground(String... contract) {

        DBHelper DBHelper = new DBHelper(MainActivity.this);
        if (DBHelper.validateContract(strContract) < 1) rval = false;

        return rval;
    }


    @Override
    protected void onPostExecute(Boolean rval){

        if(rval){
            // Hide Keyboard
            View view = MainActivity.this.getCurrentFocus();
            if(view != null){
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
            if (MainActivityWeakRef.get() != null && !MainActivityWeakRef.get().isFinishing()) {
                // Confirm Details
                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                alert.setTitle("Confirm Trip");
                alert.setMessage("CONTRACT: " + strContract + "\nTRIP: " + strTrip + "\nDATE: " + strDate);

                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        // Send SMS
                        sendSMS(PHONE_NUMBER, strMessage);

                        // Clear Fields
                        txtContract.setText("");
                        txtTrip.setText("");
                        txtDate.setText(today);
                    }
                });

                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    // Cancelled
                    }
                });

                // Show Alert
                alert.show();
            }
        }
        else{
            txtContract.setError("Invalid contract #");
            Toast.makeText(MainActivity.this, "You may need to update contracts.",
                    Toast.LENGTH_LONG).show();
        }

    }

}


// Send SMS
private void sendSMS(String phoneNumber, String message) {

    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

}

最佳答案

Android 6.0的运行时权限模型主要分为部分

<强>1。检查权限

<强>2。请求许可

你可以在你的 Activity 中为这个东西创建两个方法,如下所示

检查权限

private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS);
    if (result == PackageManager.PERMISSION_GRANTED){

        return true;

    } else {

        return false;

    }
}

请求许可

private void requestPermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.READ_SMS)){

        Toast.makeText(context,"Read Sms Allowed.",Toast.LENGTH_LONG).show();

    } else {

        ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.READ_SMS},PERMISSION_REQUEST_CODE);
    }
}

最后但同样重要的是,您需要覆盖 onRequestPermissionsResult 方法

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

                Snackbar.make(view,"Permission Granted, Now you can access SMS.",Snackbar.LENGTH_LONG).show();

            } else {

                Snackbar.make(view,"Permission Denied, You cannot access SMS.",Snackbar.LENGTH_LONG).show();

            }
            break;
    }
}

正如你问的我需要在线程中运行这个吗..答案是否定的只需在主线程中执行

关于android - 如何在现有应用程序上实现 Android 6.0 运行时权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34959229/

相关文章:

android - 无法使用 "Error XA5209: Unzipping failed"构建 Xamarin.Android 项目

Android M - GoogleAccountCredential setSelectedAccount 不起作用 - 名称不能为空

android - Android M Doze 状态本身是否有多个状态?

Android 5.0+ getRunningTasks 已弃用

android - 判断设备是否正在运行 MIUI 软件

android - 设置通知中应用程序名称文本的颜色 (Android)

android - 如何在单个 recyclerView 中设置两个不同的适配器?

java - org.json.JSONArray 类型的值无法转换为 JSONObject

java - 请求位置的运行时权限时

android - 请求运行时权限后 fragment 变为空白