'Android Studio Intent getIntExtra not Working
I have a foreground service in which the notification sends Intents to a Broadcast Receiver, but the extra content of the intent are not been passed to the broadcast receiver.
In the service:
private void proceedToStartService(Intent intent) {
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(SHOW_APPLICATION_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
notificationIntent.putExtra(NotificationReceiver.REQUEST,NotificationReceiver.ACTIVITY);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Intent startStopIntent = new Intent(this,NotificationReceiver.class);
startStopIntent.putExtra(NotificationReceiver.REQUEST,NotificationReceiver.START_STOP_FISHING);
PendingIntent startStopPendingIntent = PendingIntent.getBroadcast(this,0,startStopIntent,0);
RemoteViews collapsedView = new RemoteViews(getPackageName(),R.layout.notification_collapsed);
RemoteViews expandedView = new RemoteViews(getPackageName(),R.layout.notification_expanded);
expandedView.setOnClickPendingIntent(R.id.ivNotificationStartStop,startStopPendingIntent);
DftOp.showMessageLong(" intent "+startStopIntent.getExtras().getInt(NotificationReceiver.REQUEST,666));
Notification notification = new NotificationCompat.Builder(this, APP.CHANNEL_ID)
.setContentTitle("Fishing Tracker")
.setCustomContentView(expandedView)
.setCustomBigContentView(expandedView)
.setContentText(input)
.setSmallIcon(R.drawable.go_fishing_black)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build();
startForeground(SERVICE_ID, notification);
addListeners();
And in the broadcast receiver
public class NotificationReceiver extends BroadcastReceiver {
public static final int NEXT_EQUIPMENT = 1;
public static final int PREVIOUS_EQUIPMENT = 2;
public static final int START_STOP_FISHING = 3;
public static final int ACTUAL_EQUIPMENT = 5;
public static final int ACTIVITY = 999;
public static final int NONE = 10;
public static final String REQUEST = "requestAction";
@Override
public void onReceive(Context context, Intent intent) {
int request = intent.getIntExtra(REQUEST,890);
switch (request) {
case NONE: DftOp.showMessageLong(" NONE "); break;
case NEXT_EQUIPMENT: DftOp.showMessageLong(" next "); break;
case PREVIOUS_EQUIPMENT: DftOp.showMessageLong(" previous "); break;
case START_STOP_FISHING: DftOp.showMessageLong(" start/stop "); break;
case ACTUAL_EQUIPMENT: DftOp.showMessageLong(" actual "); break;
default: DftOp.showMessageLong("Requested= " +request); break;
}
Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(closeIntent);
}
}
But always the value is the default 890... I also tried use intent.getExtras().getInt() but the result is the same.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
