尝试使用smsmanager发送短信时app崩溃了

时间:2015-12-03 20:59:37

标签: android

我正在构建一个发送短信和接收发送报告的应用。我在onclick属性为m1的表单上有一个按钮,当我单击该按钮时,应用程序崩溃。我做错了什么?

这是代码:

public class m1g extends AppCompatActivity {
    private BroadcastReceiver sendBroadcastReceiver;
    private BroadcastReceiver deliveryBroadcastReceiver;
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";


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


        TextView rec1 = (TextView) findViewById(R.id.rec1);
        TextView pawn = (TextView) findViewById(R.id.pawn);
        TextView paw = (TextView) findViewById(R.id.paw);
        TextView paa = (TextView) findViewById(R.id.paa);
        TextView ob = (TextView) findViewById(R.id.ob);
        TextView re1 = (TextView) findViewById(R.id.re1);

        Intent i = getIntent();
        Intent ii = getIntent();
        Intent iii = getIntent();
        ob.setText(ii.getStringExtra("tex"));
        rec1.setText(i.getStringExtra("text"));
        re1.setText(iii.getStringExtra("te"));


        pawn.setPaintFlags(pawn.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        paw.setPaintFlags(paw.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        paa.setPaintFlags(paa.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);


        sendBroadcastReceiver = new BroadcastReceiver() {

            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS Sent", 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;
                }
            }
        };

        deliveryBroadcastReceiver = new BroadcastReceiver() {
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS Delivered", Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
        registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
        registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
    }

        public void m1(){
            String phoneNumber = "08039123061";
            String message = "mad";
            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);
        }
    @Override
    protected void onStop()
    {
        unregisterReceiver(sendBroadcastReceiver);
        unregisterReceiver(deliveryBroadcastReceiver);
        super.onStop();
    }

}

2 个答案:

答案 0 :(得分:1)

检查您的清单并添加此权限

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

答案 1 :(得分:1)

m1()方法的签名错误。它的声明应该是:

public void m1(View v)