Results 1 to 8 of 8
  1. #1
    Acon94 is offline Member
    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0

    Default String passed through intent resolving back to null

    Hi i am having a bit of trouble passing a string from an activity to a broadcast reciver.

    Java Code:
    
                 String string ="Passed"+weather;
                Intent passintent = new Intent("MyReceiver");
                passintent.putExtra("string", string);
                sendBroadcast(passintent);
    the above code in my activity

    then in my reciver i have

    Java Code:
                String sample = intent.getExtras().getString("string");
                String mydata = sample;
    this works as expected on the first time a notification fires from my broadcast receiver but the a few seconds later the notification goes again and the value is null i have been trying it for afew hours and have gotten nowhere any help is appreciated thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: String passed through intent resolving back to null

    There isn't enough information to really help. If you provide a short working example we might be
    able to assist.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Acon94 is offline Member
    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0

    Default Re: String passed through intent resolving back to null

    What i want to do is pass string from an activity to a broadcast receiver were I can use that string in a notification

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: String passed through intent resolving back to null

    I understand that. But I don't know what intent does or what getExtras() does. And knowing what they do does not show the code.
    Your posted code does not provide enough details to determine where the problem lies.

    It's like the following.

    Java Code:
    int value = add(10,2,4);
    System.out.println(value);  // prints 231
    Why does my code print 231?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: String passed through intent resolving back to null

    I just realized you said "activity." Is this an Android issue? There is a separate subforum for that. I have done very little with Android
    so you may have to wait for an answer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Acon94 is offline Member
    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0

    Default Re: String passed through intent resolving back to null

    Java Code:
    protected void onPostExecute(String weather) {
    
    
    
                weather is a value that is got from a weather a API
    
                String string ="Passed"+weather;
                Intent passintent = new Intent("MyReceiver");
                passintent.putExtra("string", string);
                sendBroadcast(passintent);
    
    
    
            }
    
    
        }
    so it gets passed to a broadcast reciver
    Java Code:
    public class MyBroadcaseciver extends BroadcastReceiver {
    
        MediaPlayer mymedia;
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
            mymedia = MediaPlayer.create(context, R.raw.alarm);
            mymedia.start();
            Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
    
            String sample = intent.getExtras().getString("string");
            String mydata = sample;
    
    
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Rain Alert")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText("from"))
                    .setContentText("Rainfall " + sample)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri);
    
    
            if (intent != null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                        PendingIntent.FLAG_ONE_SHOT);
                notificationBuilder.setContentIntent(pendingIntent);
            }
    
    
            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0, notificationBuilder.build());
    
    
            Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
    
    
        }
    
    
    }

  7. #7
    Acon94 is offline Member
    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0

    Default Re: String passed through intent resolving back to null

    thanks for looking anyway

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: String passed through intent resolving back to null

    Log the values before firing the intent.
    Also Log the value received in the Receiver.
    You should be able to trace the two.

    Also ensure your Intent isn't being broadcast from somewhere else.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Do you help me in resolving this
    By ra8i in forum New To Java
    Replies: 2
    Last Post: 12-21-2011, 04:18 AM
  2. Best way convert string to binary and back
    By torsion in forum Advanced Java
    Replies: 1
    Last Post: 03-24-2011, 08:03 AM
  3. Replies: 1
    Last Post: 02-07-2011, 11:44 AM
  4. entities are passed by value or passed by reference
    By syntrax in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 08:13 AM
  5. Null array when passed to MouseListener
    By stevemcc in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 11:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •