将一个String从Activity传递给Broadcast Receiver,然后传递给Service

时间:2014-09-15 18:31:09

标签: java android

我正在尝试将Activity的String变量传递给Service。我尝试通过Intent(putExtra()方法)传递它,但它对我不起作用。有人可以帮帮我吗提前致谢 :) 这是我的ActivityBRService实施

public class MainActivity extends Activity implements View.OnClickListener{

    private PendingIntent pendingIntent;
    private EditText event;
    private Button submit;



    @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);

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

    }

    @Override
    public void onClick(View view) {
        Calendar calendar = Calendar.getInstance();

        String eventS = event.getText().toString();

        calendar.set(Calendar.MONTH, 8);
        calendar.set(Calendar.YEAR, 2014);
        calendar.set(Calendar.DAY_OF_MONTH, 15);

        calendar.set(Calendar.HOUR_OF_DAY, 6);
        calendar.set(Calendar.MINUTE, 22);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.AM_PM,Calendar.PM);

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


        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}

public class BR extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent myIntent = new Intent(context, MyService.class);

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

}

2 个答案:

答案 0 :(得分:1)

你必须这样说:

Intent intent = new Intent(this, ViewActivity.class); 
intent.putExtra("fname", "Aaron");
intent.putExtra("lname", "Lennon");
startActivity(intent);

然后像这里一样检索它:

String fName = intent.getStringExtra("fname");
String lName = intent.getStringExtra("lname");

答案 1 :(得分:1)

首先,您需要在调用或启动服务之前设置它

Intent intent = new Intent(this, MyService.class);
intent.putExtra("prm1", val1);
intent.putExtra("prm2, val2);
startService(intent);

 Or 

Intent intent = new Intent(this, MyService.class);
Bundle bundle = intent.getExtras();
bundle.putString("prm1", val1);
bundle.putString("prm2", val2);
startService(intent);

And in the service
Intent intent = getIntent();
if((null != intent) && (null != intent.getExtras()) {
      Bundle bundle = intent.getExtras();
      String prm1Value = bundle.getString(prm1);
      String prm2Value = bundle.getString(prm2);
}