我创建了两个 Activity 页面。 MainActivity 和 Proceed Activity 。我在 MainActivity 页面中从用户那里获取数据,并通过 Intent 发送以继续进行 Activity 。
继续页面我正在输入电话号码,然后单击发送按钮。它成功发送了消息,但之后我创建了开始 Activity 以移动到主 Activity 页面。它正在移动到主要 Activity ,但几秒钟后它会自动进入继续 Activity 。
1.在 mainActivity 中,我正在发送类似的列表
public void Proceed(View view){
if(planetsList.size()==0){
Toast.makeText(MainActivity.this, "Please Add Product", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(MainActivity.this, Proceed.class);
intent.putExtra("list_product", planetsList);
Log.e("gjdgfl",Integer.toString(planetsList.size()));
startActivity(intent);
}
2.在进行页面编码是
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String no = mnumber.getText().toString();
Float netTotal = 0.1f;
ArrayList<Planet> myList = getIntent().getParcelableArrayListExtra("list_product");
Log.e("hai1",Integer.toString(myList.size()));
StringBuilder sb = new StringBuilder();
for (Planet temp : myList) {
sb.append(temp.getName()).append(" ").append(temp.getPerkg()).append("*").append(temp.getTotkg()).append("=").append(temp.getTotamt()).append("\n");
netTotal += temp.getTotamt();
}
sb.append("Total Amount is -"+netTotal);
System.out.println(sb.toString());
System.out.println(no);
String msg = sb.toString();
Intent intent=new Intent(getApplicationContext(),Proceed.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(no, null, msg, pi,null);
Toast.makeText(getApplicationContext(), "Message Sent successfully!", Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(Proceed.this);
builder.setTitle("Confirmation");
builder.setMessage("Message Sent Successfully")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent1 = new Intent(Proceed.this,MainActivity.class);
intent1.putExtra("exit_on_sent", true);
startActivity(intent1);
}
});
AlertDialog alert = builder.create();
alert.show();
}
最佳答案
读这个:
If you give the foreign application an Intent, and that application sends/broadcasts the Intent you gave, they will execute the Intent with their own permissions. But if you instead give the foreign application a PendingIntent you created using your own permission, that application will execute the contained Intent using your application's permission.
问题来了:
You are sending the intent as go to Proceed.class in Pending intent thus when message is done it goes to Proceed.class
Intent intent=new Intent(getApplicationContext(),Proceed.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
SmsManager sms=SmsManager.getDefault();
改变这一行:
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
关于android - 发送短信后 android Activity 自动移动到同一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43605220/