在activity中运行两个android服务并从这两个服务中获取数据,然后在方法中使用哪一个?
我正在创建一个Android应用程序,在主活动上启动两个运行的服务,这两个服务将依次向每个活动传递一个字符串值。我已经创建了一个广播接收器来接收来自每个服务的数据。我的问题是,我想运行一个通知方法,在接收到两个字符串值之后,比较这两个字符串值。在启动这两个服务之后,我尝试过运行这个方法,但是我一直得到一个空指针异常。我想知道你是否可以使用一个广播接收器从不同的服务接收数据,因为如果可能的话,我可以在onReceive方法中运行这个方法。或者有什么方法可以解决这个空指针错误 以下是我的主要活动的onStart方法和通知方法:在activity中运行两个android服务并从这两个服务中获取数据,然后在方法中使用哪一个?,android,nullpointerexception,android-service,Android,Nullpointerexception,Android Service,我正在创建一个Android应用程序,在主活动上启动两个运行的服务,这两个服务将依次向每个活动传递一个字符串值。我已经创建了一个广播接收器来接收来自每个服务的数据。我的问题是,我想运行一个通知方法,在接收到两个字符串值之后,比较这两个字符串值。在启动这两个服务之后,我尝试过运行这个方法,但是我一直得到一个空指针异常。我想知道你是否可以使用一个广播接收器从不同的服务接收数据,因为如果可能的话,我可以在onReceive方法中运行这个方法。或者有什么方法可以解决这个空指针错误 以下是我的主要活动的o
String currentWeather;
String tweet;
...
@Override
protected void onStart()
{
receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
currentWeather = intent.getStringExtra("weatherconditions");
}
};
auroraReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
tweet = intent.getStringExtra("latestTweet");
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WeatherService.BROADCAST);
registerReceiver(receiver, intentFilter);
IntentFilter auroraFilter = new IntentFilter();
intentFilter.addAction(AuroraService.MY_ACTION);
registerReceiver(auroraReceiver, auroraFilter);
Intent newService = new Intent(getApplicationContext(), WeatherService.class);
startService(newService);
Intent auroraService = new Intent(getApplicationContext(), AuroraService.class);
startService(auroraService);
sendAuroraNotification();
super.onStart();
}
public void sendAuroraNotification()
{
if ((currentWeather.equals("Clear")) && (tweet.contains("red alert") || tweet.contains("amber alert")))
{
Intent thisIntent = new Intent(this, MyActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, thisIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notificationicon)
.setContentTitle("Aurora Borealis")
.setContentText("You can now view the Aurora Borealis in your area.")
.setContentIntent(pIntent);
notif.notify(001, builder.build());
} else
{
myText.setText("No Notification yet");
}
}
在两个my Service类中发送数据的代码都很简单,并在onStartCommand方法中运行:
Intent sendInfo = new Intent();
sendInfo.setAction(BROADCAST);
sendInfo.putExtra("my_key", theString);
sendBroadcast(sendInfo);
编辑:
编辑2:
我已将代码更改为在OnReceive方法中从同一广播接收器接收两个字符串,然后在此之后调用sendAuroraNotification方法。但是,我仍然会遇到这样的错误:
07-25 17:11:36.413 5860-5860/com.example.Clear_Skies E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.Clear_Skies, PID: 5860
java.lang.RuntimeException: Error receiving broadcast Intent { act=receive_data flg=0x10 (has extras) } in com.example.Clear_Skies.MyActivity$1@423e5378
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:862)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.Clear_Skies.MyActivity.sendAuroraNotification(MyActivity.java:149)
at com.example.Clear_Skies.MyActivity$1.onReceive(MyActivity.java:78)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:848)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
以下是onStart方法的更新代码:
@Override
protected void onStart()
{
receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String currentWeather = intent.getStringExtra("weatherconditions");
String tweet = intent.getStringExtra("latestTweet");
sendAuroraNotification(currentWeather);
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_DATA);
registerReceiver(receiver, intentFilter);
Intent newService = new Intent(this, WeatherService.class);
startService(newService);
Intent auroraService = new Intent(this, AuroraService.class);
startService(auroraService);
super.onStart();
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WeatherService.WEATHER_ACTION);
intentFilter.addAction(AuroraService.AURORA_ACTION);
registerReceiver(receiver, intentFilter);
logcat表示调用sendAuroraNotification方法时会发生空指针错误
编辑3:
我已通过以下更改成功解决了该问题:
启动方法:
@Override
protected void onStart()
{
receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String currentWeather = intent.getStringExtra("weatherconditions");
String tweet = intent.getStringExtra("latestTweet");
sendAuroraNotification(currentWeather);
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_DATA);
registerReceiver(receiver, intentFilter);
Intent newService = new Intent(this, WeatherService.class);
startService(newService);
Intent auroraService = new Intent(this, AuroraService.class);
startService(auroraService);
super.onStart();
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WeatherService.WEATHER_ACTION);
intentFilter.addAction(AuroraService.AURORA_ACTION);
registerReceiver(receiver, intentFilter);
广播接收器的OnReceive方法:
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
String tweet="",currentWeather="";
if (action.equalsIgnoreCase(AuroraService.AURORA_ACTION))
{
tweet = intent.getStringExtra("latestTweet");
}
if (action.equalsIgnoreCase(WeatherService.WEATHER_ACTION))
{
currentWeather = intent.getStringExtra("weatherconditions");
}
sendAuroraNotification(tweet,currentWeather);
}
我的理解是,您希望检查从两个不同服务获得的两个字符串,并对它们进行比较。如果我没说错的话,你可以用同一个广播接收器来做这件事。使用相同的广播从您的服务接收值,一旦您获得这两个值,您就可以调用您的方法
sendAuroraNotification()
处理字符串。还要检查您是否正确初始化了notify object和textview对象。myText.setTextNo Notification这是否在activity类或service类中?@IllegalArgument setText在activity类中,主要是为了测试MyActivity。java:166是notif.notify001,builder.build?可能是您忘了初始化notify objectI,为此我更改了代码。然而,我仍然收到一个错误,请参见上文,我测试了通过将值设置为TextView来查看是否接收到数据,这起到了作用,但一个字符串只有几行长,并且在几秒钟后一直消失。显示的意思是???发布您的更新代码,并指定您在哪一行获得错误。我现在发布了更新代码。消失意味着最初字符串在屏幕上出现了几秒钟,然后textView将没有任何文本。在调用sendAuroraNotification方法之前,您需要检查您是否具有这两个值