Android 通过广播发送数据
服务Android 通过广播发送数据,android,ioio,Android,Ioio,服务 public class MainActivity extends Activity implements SurfaceHolder.Callback { private Intent intent; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
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);
}}
}
}
我试图使用广播将一个整数值从服务发送到活动,并在活动中的文本框中显示该整数值。这很简单,但我的程序还是崩溃了。这是我代码的一部分,我添加了广播代码。谁能告诉我这有什么问题吗?还有什么需要添加的吗?在清单中声明服务
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);
}
在onCreate()中更改以下行:
<service
android:name=".Service">
</service>
为此:
intent = new Intent(this, Service.class);
startService(new Intent(this, Service.class));
编辑:
intent = new Intent(this, Service.class);
startService(intent);
并在活动的onreceive()方法中获取值
在活动中注册广播:
//send broadcast from activity to all receivers listening to the action
Intent intent = new Intent();
intent.putExtra("VALUE", 100);
sendBroadcast(intent);
发布您的日志,同时发布您的IOIOS服务代码。@sush它已经存在了der@user3040168是的,它在那里,但是它被延长了。原始代码位于IOIOService类中。希望扩展服务类及其正确性,否则用这段代码很难找出问题所在。看看我的答案。希望它能帮助我们更早地尝试它…仍然认为问题在于这行Intent-Intent=new-Intent(this,MainActivity.class)代码>在您的服务类中。因为它无法获取上下文。只需注释这一行,然后选中@User3040168“是”,它工作时没有崩溃,但服务中的代码没有执行……整数没有传递。在代码中,intent为null,这就是为什么它没有得到值的原因。@GrlHu但当我命令intent intent=new intent(this,MainActivity.class);,它要求初始化“意图”意图;。。。dis行要求初始化“意图”…因为意图意图=新意图(这是MainActivity.class);不使用
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);
}
};
}
// 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;