0

我的应用程序需要启动一个应用程序并将我的数据发送给它。我用它来启动应用程序(新的和后台的):

Intent wakeIntent = new Intent(Intent.ACTION_MAIN);

wakeIntent.putExtra("type", type);

wakeIntent.putExtra("scheduleId", id);
wakeIntent.addCategory(Intent.CATEGORY_LAUNCHER);

//welcome is launcher of the target app                                                 

wakeIntent.setClass(getApplicationContext(), WelcomeActivity.class);
wakeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(wakeIntent);

当我启动应用程序作为新应用程序时,WelcomeActivity 可以接收意图中的数据“类型”,“id”,但是如果应用程序已经启动并切换了后台,则会发生唤醒的后台应用程序无法接收数据。如何

最好的问候

4

1 回答 1

1

您可以通过在从当前活动启动之前Shared Preferences存储 "type","id"在共享首选项中来执行此操作:WelcomeActivity

例如,我开始WelcomeActivity点击按钮FirstActivity

public class FirstActivity extends Activity {
SharedPreferences myPrefs;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       button.setOnClickListener(new OnClickListener() {
       void onClick() {
         //Create 

        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();
        prefsEditor.putString("type", type);
        prefsEditor.putString("scheduleId", scheduleId);
        prefsEditor.commit();

       //start WelcomeActivity here
    Intent wakeIntent = new Intent(Intent.ACTION_MAIN);

    wakeIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    //welcome is launcher of the target app                                                 

    wakeIntent.setClass(getApplicationContext(), WelcomeActivity.class);
    wakeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(wakeIntent);

        }
    });
    }
}

并在WelcomeActivityActivitySharedPreferences中将其 onCreateonResume为:

public class FirstActivity extends Activity {
SharedPreferences myPrefs;

public static boolean status=false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // this will read when first time start
        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        String strtype = myPrefs.getString("type", "nothing");
        String strscheduleId = myPrefs.getString("scheduleId", "0");
        status=true;

    }

    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
       if(status!=true){
                 // this will read when first time start
        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        String strtype = myPrefs.getString("type", "nothing");
        String strscheduleId = myPrefs.getString("scheduleId", "0");
       }
    }

    @Override
    protected void onPause() {
        super.onPause();
     // Another activity is taking focus (this activity is about to be "paused").

        // reset counter here
        status=false;
    }
}
于 2012-12-11T16:05:03.033 回答