我需要能够判断一个衍生活动(通过意图)何时完成,我该怎么做?
这就是我所拥有的:
alertDialog.setButton2("Text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String uri = "smsto:" + "";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
intent.putExtra("sms_body", PASSWORD_GENERATOR
.generatePasswordForSeed(seedText, hourToUse));
intent.putExtra("compose_mode", true);
// -- open the text message activity
startActivity(intent);
// -- I need to reset the calling activity now, but AFTER the text message activity has completed. Right now the SMS closes right away as I have no wait in...
finish();
startActivity(getIntent());
}
});
根据以下建议,我做了一些修改。然而,现在,一旦文本被发送,启动的SMS活动就“坐在那里”。我无法弄清楚如何让它返回到调用活动。这就是我所拥有的:
alertDialog.setButton2("Text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String uri = "smsto:" + "";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
intent.putExtra("sms_body", PASSWORD_GENERATOR
.generatePasswordForSeed(seedText, hourToUse));
intent.putExtra("compose_mode", true);
startActivityForResult(intent, Activity.RESULT_OK);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
startActivity(getIntent());
}
}, new IntentFilter("SMS_SENT"));
ContentResolver contentResolver = getContentResolver();
Handler handler = new Handler();
contentResolver.registerContentObserver(Uri
.parse("content://sms"), true, new ContentObserver(
handler) {
@Override
public boolean deliverSelfNotifications() {
setResult(Activity.RESULT_OK);
finish();
return super.deliverSelfNotifications();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
setResult(Activity.RESULT_OK);
finish();
}
});
}
});
alertDialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
finish();
startActivity(getIntent());
}
答案 0 :(得分:10)
使用startActivityForResult(intent, positiveinteger);
在您认为活动成功完成后,在已启动的活动中执行您想要执行的操作,执行setResult(Activity.RESULT_OK);
finish();
在开始覆盖onActivityResult()
方法的活动中。并测试您的被叫活动是否完成了它的工作。
永远不会将Activity.RESULT_OK
用于requestCode
,因为它是负常数。文档说每个正整数onActivityResult()
都会调用requestCode
方法:
重写并遵循此
如何开始结果活动:
static final int STATIC_RESULT=2; //positive > 0 integer.
Intent i = new Intent("my.activity.startNewOne");
i.putExtra("category", category);
startActivityForResult(i, STATIC_RESULT);
在您的startNewOne
活动中,您完成的结果如下:
//after the job is done, use the method i mentioned in the comments to check if the sms is delivered/submitted. and proceed if true with this
if(smsObject.getStatus()==0)//by the docs means successfuly sent
{
Intent i = getIntent(); //get the intent that has been called, i.e you did called with startActivityForResult();
i.putExtras(b);//put some data, in a bundle
setResult(Activity.RESULT_OK, i); //now you can use Activity.RESULT_OK, its irrelevant whats the resultCode
finish(); //finish the startNewOne activity
}
如何使用onActivityResult()
方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == STATIC_RESULT) //check if the request code is the one you've sent
{
if (resultCode == Activity.RESULT_OK)
{
// this is successful mission, do with it.
{
} else
// the result code is different from the one you've finished with, do something else.
}
}
super.onActivityResult(requestCode, resultCode, data);
}
答案 1 :(得分:5)
第二个问题的后续答案: 出于好奇,您是否考虑过发送文本而不创建活动?我问的原因是因为从我可以看出,似乎没有用户体验在您的SMS活动中发生。如果您没有视图和用户交互,那么可能只是定义一个线程或创建一个帮助程序类就可以完成这项工作。
第一个问题的原始答案: 尝试使用startActivityForResult(Intent,int)。当文本消息活动结束时,您可以收到当前活动的通知。
答案 2 :(得分:2)
很遗憾,您必须使用SmsManager
使用此:SMS Messaging in Android | mobiForge
您可以收到SMS_SENT
注意强> 的:
确保使用onCreate()
答案 3 :(得分:1)
要回答您的新问题,您可以在意图中添加exit_on_sent
,这会导致在发送邀请后返回SMS活动。
smsIntent.putExtra("exit_on_sent", true);
不幸的是,看起来它仍然会返回resultCode
Activity.RESULT_CANCELLED
,无论你是这样做还是按下了后退按钮,所以你无法判断用户是否真的发送了文字与否。