将数据从Activity传递到Service

时间:2014-09-15 20:21:45

标签: java android

Service传递String到Activity时遇到问题。我想要做的是每次拨打Service时传递不同的字符串。我每次都使用Intent(putExtra()方法)。问题是每次它显示我传递的第一个String。有没有办法实现呢?

以下是我的ActivityBroadcast ReceiverService

public class MainActivity extends Activity implements View.OnClickListener{

private PendingIntent pendingIntent;
private EditText event;
private Button submit, date, time;
private static final int DIALOG_DATE = 1;
private static final int DIALOG_TIME = 2;

private int myYear = 2014;
private int myMonth = 8;
private int myDay = 16;
private int myHour = 6;
private int myMinute = 30;

private List<String> events;
private List<Calendar> dateTimes;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    event = (EditText)findViewById(R.id.event);
    submit = (Button)findViewById(R.id.submit);
    submit.setOnClickListener(this);
    date = (Button)findViewById(R.id.date);
    date.setOnClickListener(this);
    time = (Button)findViewById(R.id.time);
    time.setOnClickListener(this);

    events = new ArrayList<String>();
    dateTimes = new ArrayList<Calendar>();

    //List<Calendar> events = new ArrayList<Calendar>();

}

@Override
protected Dialog onCreateDialog(int id) {
    if(id == DIALOG_DATE) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, myCallBack, myYear, myMonth, myDay);
        return datePickerDialog;
    }else {
        TimePickerDialog timePickerDialog = new TimePickerDialog(this, myCallBack1, myHour, myMinute, true);
        return timePickerDialog;
    }
}

DatePickerDialog.OnDateSetListener myCallBack = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        myYear = year;
        myMonth = month;
        myDay = day;
    }
};
TimePickerDialog.OnTimeSetListener myCallBack1 = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker timePicker, int hour, int minute) {
        myHour = hour;
        myMinute = minute;
    }
};

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.date:
            showDialog(DIALOG_DATE);
            break;
        case R.id.time:
            showDialog(DIALOG_TIME);
            break;
        case R.id.submit:
            String eventS = event.getText().toString();

            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.MONTH, myMonth);
            calendar.set(Calendar.YEAR, myYear);
            calendar.set(Calendar.DAY_OF_MONTH, myDay);

            calendar.set(Calendar.HOUR_OF_DAY, myHour);
            calendar.set(Calendar.MINUTE, myMinute);
            calendar.set(Calendar.SECOND, 0);
//                calendar.set(Calendar.AM_PM, Calendar.PM);

            Intent myIntent = new Intent(MainActivity.this, BR.class);
            myIntent.putExtra("event", eventS);
            pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
            event.setText("");
            break;

    }
}

public class BR extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent myIntent = new Intent(context, MyService.class);
    intent.getExtras().getString("event");
    myIntent.putExtra("event", intent.getStringExtra("event"));

    context.startService(myIntent);
    Log.i("myLogs", "BR started");
}
}

public class MyService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
    String event = intent.getExtras().getString("event");

    Notification notification = new Notification(R.drawable.ic_launcher, "This is a test message!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(), event, event, pendingNotificationIntent);

    mManager.notify(0, notification);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

1 个答案:

答案 0 :(得分:-1)

服务无法停止直至完成。您必须等待当前服务完成才能发送其他数据。您可以收集所有字符串并将它们全部发送或等到后台服务完成