jav*_*y79 7 android android-activity
我需要能够判断一个衍生活动(通过意图)何时完成,我该怎么做?
这就是我所拥有的:
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());
}
});
Run Code Online (Sandbox Code Playgroud)
根据以下建议,我做了一些修改.然而,现在,一旦文本被发送,启动的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());
}
Run Code Online (Sandbox Code Playgroud)
Nik*_*ski 10
使用 startActivityForResult(intent, positiveinteger);
当您认为活动成功完成时,在已启动的活动中执行您想要执行的操作 setResult(Activity.RESULT_OK); finish();
并且在开始覆盖onActivityResult()方法的活动中.并测试您的被叫活动是否完成了它的工作.
真的再也不能使用Activity.RESULT_OK了requestCode,因为它是负常数.Docs说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);
Run Code Online (Sandbox Code Playgroud)
在您的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
}
Run Code Online (Sandbox Code Playgroud)
你如何使用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);
}
Run Code Online (Sandbox Code Playgroud)
第二个问题的后续答案: 出于好奇,您是否考虑过发送文本而不创建活动?我问的原因是因为从我可以看出,似乎没有用户体验在您的SMS活动中发生.如果您没有视图和用户交互,那么可能只是定义一个线程或创建一个帮助程序类就可以完成这项工作.
第一个问题的原始答案: 尝试使用startActivityForResult(Intent,int).当文本消息活动完成时,您可以收到当前活动的通知.
| 归档时间: |
|
| 查看次数: |
12875 次 |
| 最近记录: |