android sms许可marshmallow

时间:2017-03-15 01:07:28

标签: java android permissions sms

我的应用程序出现问题,因为我不知道“弹出,发送短信的权限,存储访问权限(将信息存储在手机中)。”

这是我的代码

XML

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="School Supplies"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Product"
    android:id="@+id/textView2"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="48dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hamburger"
    android:id="@+id/proHam"
    android:layout_marginTop="34dp"
    android:layout_below="@+id/textView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cheese Burger"
    android:id="@+id/proCheese"
    android:layout_below="@+id/txtHam"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="32dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="French Fries"
    android:id="@+id/proFries"
    android:layout_below="@+id/txtFries"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="2dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Soft Drinks"
    android:id="@+id/proDrinks"
    android:layout_above="@+id/btnCompute"
    android:layout_alignRight="@+id/proHam"
    android:layout_alignEnd="@+id/proHam"
    android:layout_marginBottom="43dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Unit Price"
    android:id="@+id/textView7"
    android:layout_alignTop="@+id/textView2"
    android:layout_toRightOf="@+id/proCheese"
    android:layout_toEndOf="@+id/proCheese"
    android:layout_marginLeft="30dp"
    android:layout_marginStart="30dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="25.00"
    android:id="@+id/lblHam"
    android:layout_alignTop="@+id/proHam"
    android:layout_alignLeft="@+id/textView7"
    android:layout_alignStart="@+id/textView7" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="35.00"
    android:id="@+id/lblCheese"
    android:layout_alignTop="@+id/proCheese"
    android:layout_alignLeft="@+id/lblHam"
    android:layout_alignStart="@+id/lblHam" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="25.00"
    android:id="@+id/lblFries"
    android:layout_alignTop="@+id/proFries"
    android:layout_alignLeft="@+id/btnSend"
    android:layout_alignStart="@+id/btnSend" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="20.00"
    android:id="@+id/lblDrinks"
    android:layout_alignTop="@+id/proDrinks"
    android:layout_alignLeft="@+id/lblFries"
    android:layout_alignStart="@+id/lblFries" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Quantity"
    android:id="@+id/textView12"
    android:layout_alignTop="@+id/textView7"
    android:layout_toRightOf="@+id/textView7"
    android:layout_toEndOf="@+id/textView7"
    android:layout_marginLeft="29dp"
    android:layout_marginStart="29dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtHam"
    android:inputType="number"
    android:layout_alignBottom="@+id/lblHam"
    android:layout_alignLeft="@+id/textView12"
    android:layout_alignStart="@+id/textView12"
    android:layout_alignRight="@+id/textView12"
    android:layout_alignEnd="@+id/textView12" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtCheese"
    android:inputType="number"
    android:layout_alignBottom="@+id/lblCheese"
    android:layout_alignLeft="@+id/txtHam"
    android:layout_alignStart="@+id/txtHam"
    android:layout_alignRight="@+id/txtHam"
    android:layout_alignEnd="@+id/txtHam" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtFries"
    android:inputType="number"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/txtCheese"
    android:layout_alignStart="@+id/txtCheese"
    android:layout_alignRight="@+id/txtCheese"
    android:layout_alignEnd="@+id/txtCheese" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtDrinks"
    android:inputType="number"
    android:layout_alignBottom="@+id/lblDrinks"
    android:layout_alignLeft="@+id/txtFries"
    android:layout_alignStart="@+id/txtFries"
    android:layout_alignRight="@+id/txtFries"
    android:layout_alignEnd="@+id/txtFries" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Subtotal"
    android:id="@+id/textView13"
    android:layout_alignTop="@+id/textView12"
    android:layout_toRightOf="@+id/textView"
    android:layout_toEndOf="@+id/textView"
    android:layout_marginLeft="22dp"
    android:layout_marginStart="22dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0.00"
    android:id="@+id/lblHam2"
    android:layout_alignBottom="@+id/txtHam"
    android:layout_alignLeft="@+id/textView13"
    android:layout_alignStart="@+id/textView13" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0.00"
    android:id="@+id/lblCheese2"
    android:layout_alignBaseline="@+id/lblCheese"
    android:layout_alignBottom="@+id/lblCheese"
    android:layout_alignLeft="@+id/lblHam2"
    android:layout_alignStart="@+id/lblHam2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0.00"
    android:id="@+id/lblFries2"
    android:layout_alignTop="@+id/lblFries"
    android:layout_alignLeft="@+id/lblCheese2"
    android:layout_alignStart="@+id/lblCheese2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0.00"
    android:id="@+id/lblDrinks2"
    android:layout_above="@+id/lblDrinks"
    android:layout_alignLeft="@+id/lblFries2"
    android:layout_alignStart="@+id/lblFries2" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Compute"
    android:id="@+id/btnCompute"
    android:onClick="doCompute"
    android:layout_marginBottom="35dp"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/proDrinks"
    android:layout_alignStart="@+id/proDrinks" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send Orders"
    android:id="@+id/btnSend"
    android:onClick="doSend"
    android:layout_alignBottom="@+id/btnCompute"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0.00"
    android:id="@+id/lblTotal"
    android:layout_alignBottom="@+id/btnSend"
    android:layout_alignLeft="@+id/lblDrinks2"
    android:layout_alignStart="@+id/lblDrinks2" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:text="Total"
    android:layout_above="@+id/lblTotal"
    android:layout_alignRight="@+id/lblTotal"
    android:layout_alignEnd="@+id/lblTotal" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignBottom="@+id/proDrinks"
    android:layout_alignParentEnd="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:layout_alignTop="@+id/proHam"
    android:layout_alignStart="@+id/proDrinks"
    android:background="@drawable/a"
    android:layout_toStartOf="@+id/textView"
    android:layout_above="@+id/lblCheese"
    android:layout_alignEnd="@+id/proHam" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView3"
    android:background="@drawable/b"
    android:layout_below="@+id/imageView2"
    android:layout_above="@+id/txtFries"
    android:layout_toStartOf="@+id/textView7" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView4"
    android:background="@drawable/c"
    android:layout_below="@+id/imageView3"
    android:layout_above="@+id/txtDrinks"
    android:layout_toStartOf="@+id/textView" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView5"
    android:layout_alignTop="@+id/proDrinks"
    android:layout_alignParentStart="true"
    android:background="@drawable/d"
    android:layout_above="@+id/editText"
    android:layout_alignEnd="@+id/imageView4" />

JAVA

package com.example.jinzuken1.myapplication;`

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {`

Button btnCompute, btnSend;
EditText txtHam, txtCheese, txtFries, txtDrinks;
TextView lblHam, lblCheese, lblFries, lblDrinks, lblHam2, lblCheese2, lblFries2, lblDrinks2, lblTotal, productHam, productCheese, productFries, productDrinks;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnCompute = (Button) findViewById(R.id.btnCompute);
    btnSend = (Button) findViewById(R.id.btnSend);

    txtHam = (EditText) findViewById(R.id.txtHam);
    txtCheese = (EditText) findViewById(R.id.txtCheese);
    txtFries = (EditText) findViewById(R.id.txtFries);
    txtDrinks = (EditText) findViewById(R.id.txtDrinks);

    lblTotal = (TextView) findViewById(R.id.lblTotal);
    lblHam = (TextView) findViewById(R.id.lblHam);
    lblCheese = (TextView) findViewById(R.id.lblCheese);
    lblFries = (TextView) findViewById(R.id.lblFries);
    lblDrinks = (TextView) findViewById(R.id.lblDrinks);

    lblHam2 = (TextView) findViewById(R.id.lblHam2);
    lblCheese2 = (TextView) findViewById(R.id.lblCheese2);
    lblFries2 = (TextView) findViewById(R.id.lblFries2);
    lblDrinks2 = (TextView) findViewById(R.id.lblDrinks2);

    productHam = (TextView) findViewById(R.id.proHam);
    productCheese = (TextView) findViewById(R.id.proCheese);
    productFries = (TextView) findViewById(R.id.proFries);
    productDrinks = (TextView) findViewById(R.id.proDrinks);


    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SendSMSMessage();
        }
    });
    btnCompute.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            doCompute();
        }
    });
}

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

public void doCompute(){

    int lblHam = 25;
    int lblCheese = 35;
    int lblFries = 25;
    int lblDrinks = 20;

    int Total;

    int HamQ = Integer.valueOf(txtHam.getText().toString()); // unnecessary .toString(), getText() already returns string, Oops, editable pala return, sorry, haha
    int CheeseQ = Integer.valueOf(txtCheese.getText().toString());
    int FriesQ = Integer.valueOf(txtFries.getText().toString());
    int DrinksQ = Integer.valueOf(txtDrinks.getText().toString());

    int HamSub;
    int CheeseSub;
    int FriesSub;
    int DrinksSub;

    HamSub = lblHam * HamQ;
    CheeseSub = lblCheese * CheeseQ;
    FriesSub = lblFries * FriesQ;
    DrinksSub = lblDrinks * DrinksQ;

    Total = HamSub + CheeseSub + FriesSub + DrinksSub;

    lblHam2.setText(HamSub + ""); // you can also do it this way, much more concise
    lblCheese2.setText(Integer.toString(CheeseSub));
    lblFries2.setText(Integer.toString(FriesSub));
    lblDrinks2.setText(Integer.toString(FriesSub));

    lblTotal.setText(Integer.toString(Total));
}

public void SendSMSMessage(){
    Log.i("Send SMS", "");
    String phoneNo = "09470018219";
    String msg =  productHam.getText().toString() + " " + txtHam.getText().toString() + "pc/pcs" + " " + lblHam2.getText().toString() + "pesos" + "\n" + productCheese.getText().toString() + " " +
            txtCheese.getText().toString() + "pc/pcs" + "  " + lblCheese2.getText().toString() + "pesos" + "\n" + productFries.getText().toString() + " " + txtFries.getText().toString() + "pc/pcs" + " " + lblFries2.getText().toString() + "pesos" + "\n" +
            productDrinks.getText().toString() + " " + txtDrinks.getText().toString() + "pc/pcs" + " " + lblDrinks2.getText().toString() + "pesos" + "\n" + "Total: " + lblTotal.getText().toString() + "pesos";

    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, msg, null, null);
        Toast.makeText(getApplicationContext(), "SMS Sent", Toast.LENGTH_LONG).show();
    }

    catch (Exception e){
        Toast.makeText(getApplicationContext(), "SMS Failed, Try Again", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jinzuken1.myapplication" >


<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="School Supplies with SMS"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

我的朋友需要你的帮助!谢谢:))

2 个答案:

答案 0 :(得分:2)

这是一个简单的例子,在必要时检查Android版本请求权限

public void checkAndroidVersion(String phoneNumber){

    if (Build.VERSION.SDK_INT >= 23) {
        int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
        if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(RegistrationActivity.this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS);
            return;
        }else{
            sendSms(phoneNumber);
        }
    } else {
        sendSms(mobile);
    }
}

public void sendSms(String phoneNumber)
{
    // Your code goes here
}

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

                sendSms(phoneNumber);
            } else {

                Toast.makeText(RegistrationActivity.this, "SEND_SMS Denied", Toast.LENGTH_SHORT)
                        .show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

我希望它有所帮助

答案 1 :(得分:0)

试试这个,

    private static final int REQUEST = 112;


    if (Build.VERSION.SDK_INT >= 23)
    {
        String[] PERMISSIONS = {android.Manifest.permission.SEND_SMS};
            if (!hasPermissions(mContext, PERMISSIONS)) 
            {
                ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
            } else {
                SendSMSMessage();//call send msg function
            }
    } else 
    {
             SendSMSMessage();//call send msg function
    }

获取权限结果

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        SendSMSMessage();//call send msg function
                } else {
                    Toast.makeText(mContext, "The app was not allowed to send sms.", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

检查marshmallow的权限

    private static boolean hasPermissions(Context context, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

清单

        <uses-permission android:name="android.permission.SEND_SMS" />