0
 public class MainActivity extends Activity implements SurfaceHolder.Callback 
{
private Intent intent;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


       if (activityReceiver != null)
{ 
       IntentFilter intentFilter = new IntentFilter();
      registerReceiver(activityReceiver, intentFilter);
}

    intent = new Intent(this, Service.class);
    startService(intent);

    }

 private BroadcastReceiver activityReceiver = new BroadcastReceiver(){
    public void onReceive(Context context, Intent intent) {
    int value=intent.getIntExtra("VALUE", 0);
    txtData.setText(""+value);
    }}

}

SERVICE

public class Service extends IOIOService {
//Intent intent=new Intent(this, MainActivity.class);
private final int BUTTON_PIN = 34;
@Override
protected IOIOLooper createIOIOLooper() {
    return new BaseIOIOLooper() {
        private DigitalOutput led_;
        private DigitalInput mButton;
        int  oneTime = 0;



        @Override
        protected void setup() throws ConnectionLostException,
                InterruptedException {
            led_ = ioio_.openDigitalOutput(IOIO.LED_PIN);
            mButton = ioio_.openDigitalInput(BUTTON_PIN, DigitalInput.Spec.Mode.PULL_UP);
        }

        @Override
        public void loop() throws ConnectionLostException,
                InterruptedException {

             final boolean reading1 = mButton.read();
             if (reading1) 
             {
                 led_.write(false);
                 if(oneTime == 0)
                 {
                                             Intent intent = new Intent();
                     intent.putExtra("VALUE", 100);
                     sendBroadcast(intent);
                     oneTime = 1;
                 }

                          } 
             else 

             {
                               led_.write(true);
                             }
            Thread.sleep(100);
              }

    };
}
}
  @Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (intent != null && intent.getAction() != null
            && intent.getAction().equals("stop")) {
        // User clicked the notification. Need to stop the service.
        nm.cancel(0);
        stopSelf();
    } else 
    {
        // Service starting. Create a notification.
        Notification notification = new Notification(
                R.drawable.ic_launcher, "IOIO service running",
                System.currentTimeMillis());
        notification
                .setLatestEventInfo(this, "IOIO Service", "Click to stop",
                        PendingIntent.getService(this, 0, new Intent(
                                "stop", null, this, this.getClass()), 0));
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        nm.notify(0, notification);
    }
}

@Override
public IBinder onBind(Intent arg0) {
    return(null);

}

} I am trying to send an integer value from service to activity using broadcast and displaying that integer into a textbox in activity. Its a simple thing but still my program is getting crashed. This is a part of my code where i have added the code for broadcasting. Can anyone please tell me whats wrong in this ? Is there anything to add more ?

Aswathy
  • 337
  • 2
  • 8
  • 18

3 Answers3

1

In your onCreate() change the below lines :

   intent = new Intent(this, Service.class);
    startService(new Intent(this, Service.class));

to this:

intent = new Intent(this, Service.class);
startService(intent);

EDITED:

//send broadcast from activity to all receivers listening to the action 
        Intent intent = new Intent();
        intent.putExtra("VALUE", 100);
        sendBroadcast(intent);

And fetch the value in the activity's onRecieve() method.

Register the Broadcast in activity:

public class MainActivity extends Activity implements SurfaceHolder.Callback 
{
private Intent intent;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    intent = new Intent(this, Service.class);
    startService(intent);
if (activityReceiver != null)
  {
           IntentFilter intentFilter = new IntentFilter();
        //Map the intent filter to the receiver
         registerReceiver(activityReceiver, intentFilter);
  }

}

 private BroadcastReceiver activityReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(getApplicationContext(), "received message in activity..!", Toast.LENGTH_SHORT).show();
           int value=intent.getIntExtra("VALUE", 0);
           txtData.setText(value);
    }
};


 }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Tried it earlier ...still crashing – Aswathy Dec 27 '13 at 06:20
  • I think the issue is with the this line `Intent intent=new Intent(this, MainActivity.class);` in your service class. As its not able to get the context. Just comment this line and then check @user3040168 – GrIsHu Dec 27 '13 at 06:22
  • Yes it worked without crashing but the code in service is nt executed...the integer is not passed. – Aswathy Dec 27 '13 at 06:34
  • In your code the intent is null that is why its not getting the value. – GrIsHu Dec 27 '13 at 06:43
  • @GrlHu But then when i command Intent intent=new Intent(this, MainActivity.class);, it asks to intialize the "intent" – Aswathy Dec 27 '13 at 06:45
  • @GrlsHu should we declare "ACTION_STRING_ACTIVITY" as a constant that has to be passed to the activity – Aswathy Dec 27 '13 at 06:53
  • No need of that i had just shown for understanding only. Check out my updated answer. @user3040168 – GrIsHu Dec 27 '13 at 06:53
  • @GrlHu I have edited the code as you suggested..but nothin is passed..In on recieve method the value is displayed in the textbox...nothing is displayed... – Aswathy Dec 27 '13 at 07:02
  • You have not registered your activity to recieve the broadcast that is why its not getting the value. Check out the tutorial it will help you. http://androidexperinz.wordpress.com/2012/02/14/communication-between-service-and-activity-part-1/ – GrIsHu Dec 27 '13 at 07:05
  • Let me know if any query. – GrIsHu Dec 27 '13 at 07:06
  • @GrlsHu thanks i read that...am using broadcast first time....what i understood is...to recieve anything u need to create a broadcast reciever and then u need to register this reciever. am i right ? – Aswathy Dec 27 '13 at 07:31
  • @GrlsHu i have edited my code...could you pls check it out.. i am confused wid this line... IntentFilter intentFilter = new IntentFilter(); – Aswathy Dec 27 '13 at 08:04
  • Check for the null value in your broadcast reciever as i have checked in activity. – GrIsHu Dec 27 '13 at 08:15
  • @GrlsHu do we need to unregister it anywhere if it registered ? – Aswathy Dec 27 '13 at 08:21
  • Yes you need to unregister it in `onDestroy` like `@Override public void onDestroy() { super.onDestroy(); Log.d("Service", "onDestroy"); //STEP3: Unregister the receiver unregisterReceiver(activityReceiver); }` – GrIsHu Dec 27 '13 at 08:26
  • @GrlsHu on adding unregisteration also its nt working..ther is no error in logcat also hw cn i find the bug? – Aswathy Dec 27 '13 at 08:55
  • @GrlsHu logcat is error free...only the thing is it is not dispalyed – Aswathy Dec 27 '13 at 09:21
  • @GrlsHu i tried by passing a string and it worked...thanks my actual intention is done – Aswathy Dec 27 '13 at 09:37
0

declare service in manifest.

<service
                android:name=".Service">
 </service>
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0
             // cuase for null pointer exception, intent is getting set as null first
then ur using putExtra method on it.  
              Intent intent = null;
                 intent.putExtra("VALUE", 100);
                 sendBroadcast(intent);
                 oneTime = 1;
Sush
  • 3,864
  • 2
  • 17
  • 35
  • intent.putExtra("VALUE", 100);...dis line is asking for initializing the "intent"...since Intent intent=new Intent(this, MainActivity.class); is not used – Aswathy Dec 27 '13 at 06:48