gpt4 book ai didi

android - 尝试使用 smsmanager 发送短信时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 20:15:02 26 4
gpt4 key购买 nike

我正在构建一个应用程序来发送短信和接收送达报告。我在表单上有一个按钮,其 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();
}

}

最佳答案

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

public void m1(View v)

关于android - 尝试使用 smsmanager 发送短信时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34076007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com