我有一个应用程序A,它使用以下代码调用另一个应用程序B的Activity。
int request_code = 4;
Intent i = new Intent();
i.setClassName("name.of,packageB","class.name.of.packageB");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(i, request_code);
应用程序B中的目标活动已启动,此目标活动会产生另一项服务。
一旦此服务完成,我需要将结果返回给调用活动。
除了致电sendBroadcast(Intent)
我有以下代码无效。对此有任何帮助表示赞赏。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_activity_main);
String finish_activity="";
try{
Intent intent = getIntent();
if(intent!=null && intent.getExtras()!=null)
finish_activity = intent.getExtras().getString("finish_activity", "");
}catch(Exception e){
System.out.println(e.toString());
}
if(finish_activity.equals("")){
Intent intent = new Intent(this, Service.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startService(intent);
} else{
ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
Intent i = new Intent();
setResult(RESULT_OK,i);
finish();
}
}
我通过服务再次调用此Activity以返回无效的结果。 调用我的目标活动的服务代码。
Intent intent_finish = new Intent(context, TargetActivityName.class);
intent_finish.putExtra("finish_activity", "true");
intent_finish.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent_finish.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent_finish.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent_finish);
我没有在应用程序A中收到任何关于startActivityForResult的回调。对此有任何帮助。非常感谢。
我的两个应用程序的完整代码。 申请A
public class AppA extends Activity {
private Button mOnBootBtn;
private Button mSettingsBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if((requestCode == 4 )&& (resultCode==RESULT_OK)){
System.out.println("result code = "+requestCode);
}
};
public void simulate(View view){
if(view.getId() == R.id.invoke){
int request_code = 4;
Intent i = new Intent();
i.setClassName("com.example.AppB","com.example.AppB.Test");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(i, request_code);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
申请B - Test.java
public class Test extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_activity_main);
String finish_activity="";
try{
Intent intent = getIntent();
if(intent!=null && intent.getExtras()!=null)
finish_activity = intent.getExtras().getString("finish_activity", "");
}catch(Exception e){
System.out.println(e.toString());
}
if(finish_activity.equals("")){
Intent intent = new Intent(this, Service.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startService(intent);
} else{
ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
Intent i = new Intent();
setResult(RESULT_OK,i);
finish();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
}
申请表B- Service.java
public class Service extends Service {
public static Context context;
public static Intent receivedIntent;
public static String calling_intent;
@Override
public void onStart(Intent intent1, int startId) {
// TODO Auto-generated method stub
super.onStart(intent1, startId);
System.out.println("Inside Service");
if (NetworkAvailable(context)) {
Intent intent_finish = new Intent(context, Test.class);
intent_finish.putExtra("finish_activity", "true");
intent_finish.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent_finish.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent_finish.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent_finish);
}
}else{
System.out.println("Inside service - No net");
}
}
@Override
public IBinder onBind(Intent receivedIntent) {
// TODO Auto-generated method stub
return null;
}
private static boolean NetworkAvailable(Context context) {
ConnectivityManager connectivityManager;
try{
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
}
先谢谢。
答案 0 :(得分:0)
我有点惊讶你的编译器没有抱怨额外的分号。但是你错过了你方法的@Override。您的整个方法签名应如下所示:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if((requestCode == 4 )&& (resultCode==RESULT_OK)){
System.out.println("result code = "+requestCode);
}
}