check if user Has Sms Permissions - Android android.os

Android examples for android.os:UserManager

Description

check if user Has Sms Permissions

Demo Code

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.UserManager;
import android.text.TextUtils;
import com.android.messaging.Factory;
import com.android.messaging.R;
import com.android.messaging.datamodel.DataModel;
import com.android.messaging.ui.conversation.ConversationActivity;
import com.android.messaging.ui.conversationlist.ConversationListActivity;

public class Main{

    /**// w  w  w  .  java 2  s .c o m
     * Determine if the user doesn't have SMS permissions. This can happen if you are not the phone
     * owner and the owner has disabled your SMS permissions.
     * @param context is the Context used to resolve the user permissions
     * @param activity is the Activity used to launch an error Dialog if necessary
     * @return true if the user has SMS permissions, otherwise false.
     */
    private static boolean checkHasSmsPermissionsForUser(Context context,
            Activity activity) {

        UserManager userManager = (UserManager) context
                .getSystemService(Context.USER_SERVICE);
        if (userManager.hasUserRestriction(UserManager.DISALLOW_SMS)) {
            new AlertDialog.Builder(activity)
                    .setMessage(R.string.requires_sms_permissions_message)
                    .setCancelable(false)
                    .setNegativeButton(
                            R.string.requires_sms_permissions_close_button,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(
                                        final DialogInterface dialog,
                                        final int button) {
                                    System.exit(0);
                                }
                            }).show();
            return false;
        }
        return true;
    }

}

Related Tutorials