简体   繁体   中英

Pass a String from Activity to Broadcast Receiver then to Service

I am trying to pass String variable from Activity to Service . I tried to pass it by Intent (putExtra() method), however it doesn't work for me. Could someone please help me. Thanks in advance :) Here is my Activity , BR , Service implementation

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

}

You have to put it in such way:

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

And then retrieve it like here:

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

First you need to set it before calling or starting a service

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM