如何在启动后台应用程序时向其发送数据?

时间:2012-12-11 15:32:10

标签: android android-intent background

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

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可以在意图中接收数据“type”,“id”, 但如果应用程序已经启动并切换后台,则会发生唤醒的后台应用程序无法接收数据。如何

对任何

的最好问候

1 个答案:

答案 0 :(得分: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);

        }
    });
    }
}

WelcomeActivity活动中,请SharedPreferencesonCreate中的onResume为:

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;
    }
}