Reputation: 337
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 ?
Upvotes: 0
Views: 1331
Reputation: 23638
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);
}
};
}
Upvotes: 1
Reputation: 3874
// 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;
Upvotes: 0
Reputation: 17401
declare service in manifest.
<service
android:name=".Service">
</service>
Upvotes: 0