在Pending Intents Android中传递值

时间:2012-12-12 05:29:28

标签: android android-intent background-process android-pendingintent

我们可以在后台进程的挂起意图中传递参数吗。

  Intent ij = new Intent(context,DemoActivity.class);
  PendingIntent operation = PendingIntent.getActivity(getBaseContext(),0,ij,Intent.FLAG_ACTIVITY_NEW_TASK);
  AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
  GregorianCalendar calendar = new GregorianCalendar(y, m, d,hr, mi);
  long alarm_time = calendar.getTimeInMillis();
  alarmManager.set(AlarmManager.RTC_WAKEUP,alarm_time,operation);

在此,我正在使用警报管理器来启动后台进程。通过使用此方法,我可以传递任何变量或参数吗?

public class DemoActivity extends FragmentActivity {


protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState); 
/** Creating an Alert Dialog Window */
    AlertDemo alert = new AlertDemo();

    /** Opening the Alert Dialog Window */
    alert.show(getSupportFragmentManager(), "AlertDemo");       
}
}

在Alert Demo课程中,我只使用警告框。 现在帮助我,在哪里放置Put Exatra方法?..

1 个答案:

答案 0 :(得分:10)

是的,您可以在挂起的Intent中传递变量,如下所示:

            Intent in = new Intent(context, DemoActivity.class );
            in.putExtra( "notification_id", REGISTER_NOTIF_ID);  
            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            in.putExtra("2", Variable);
            in.putExtra("1", Variable);
            in.putExtra("IMData", Variable);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, in, 0);

并在您的DemoActivity类的onCreate中执行以下操作:

    Bundle extras = getIntent().getExtras();

    userGetId = extras.getString("2");
    userNameRecv = extras.getString("1");
    userFriendId = extras.getString("IMData")