- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经尝试了很长时间来弄清楚如何将数据从服务类发送到 mainactivity 类。我现在正在使用广播接收器来做这件事。问题是 MainActivity 类中没有接收到它们。
public class MyService extends Service {
//all my code for the service which takes the users gps location.
private static final String TAG = "Hello";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 10;//change this for time
private static final float LOCATION_DISTANCE = 0f;//change this to get it every distance
private static final String MY_ACTION = "MY_ACTION";
private final Handler handler = new Handler();
Intent intent;
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
//Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
//Log.e(TAG, "onLocationChanged: " + location.toString());
mLastLocation.set(location);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
//different possible uses for locations
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0) {
return null;
}
//the start of the activity sends data every 5 seconds
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
//initializes locationManager
initializeLocationManager();
try {
Toast.makeText(MyService.this, "It Works", Toast.LENGTH_SHORT).show();
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
private void initializeLocationManager() {
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 5000); // 5 seconds delay
}
};
private void DisplayLoggingInfo() {
Log.d(TAG, "entered DisplayLoggingInfo");
intent = new Intent();
initializeLocationManager();
try {
Toast.makeText(MyService.this, "It Works", Toast.LENGTH_SHORT).show();
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
intent.putExtra("Latitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
intent.putExtra("Longitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
sendBroadcast(intent);
Log.i(TAG,"intent sent");
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
}
数据已发送。但是,当我尝试从 MainActivity 访问它时,它似乎没有得到它。
public class MainActivity extends AppCompatActivity {
public SimpleLocation location;
Intent intent;
double latitude;
double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Sets views for Android
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
startService(new Intent(this, MyService.class));
intent = new Intent(this, MyService.class);
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, new IntentFilter());
Toast.makeText(MainActivity.this, "" + latitude, Toast.LENGTH_SHORT).show();
}
//sets up broadcast Reciever
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Double latitude = intent.getDoubleExtra("Latitude", 0);
Double longitude = intent.getDoubleExtra("Longitude", 0);
Log.d("hello", "It came this far");
Log.d("hello", " " + latitude);
Log.d("hello", " " + longitude);
TextView txtDateTime = (TextView) findViewById(R.id.textView);
txtDateTime.setText(latitude.toString());
}
};
@Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter());
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
}
有谁知道我做错了什么?
最佳答案
首先,你应该在发送的 Intent 中添加一个 Action ,假设是location,然后替换
intent.putExtra("Latitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
intent.putExtra("Longitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
sendBroadcast(intent);
与
intent.setAction("location");
intent.putExtra("Latitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
intent.putExtra("Longitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
并替换
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, new IntentFilter());
与
IntentFilter filter = new IntentFilter("location");
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, filter);
并且你应该在启动服务之前注册接收者,所以替换
@Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter());
}
与
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter());
startService(intent);
}
因为你已经在onResume
中注册了,所以你可以移除
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, new IntentFilter());
来自您的onCreate
方法。
关于java - Intent 未通过广播监听器注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33979331/
谁能给我提供代码或链接,以便在可能的情况下使用 UDP 发送和接收广播消息? 我一直被困在一个问题中,希望你们能帮助我解决它。谢谢 最佳答案 这是一个 C# 示例: using System; usi
我想将形状为 [a,b,c] 的张量中的元素相乘,每个元素在第 3 维中使用来自形状为 [a,b] 的张量的标量。 例如, x = |[1,2][3,4][5,6]| |[1,2][3,4][5,6]
广播是使具有不同形状的数组具有用于算术运算的兼容形状的过程。在 numpy 中,我们可以广播数组。 TensorFlow 图是否支持类似于 numpy 的广播? 最佳答案 是的,它是支持的。打开终端并
我有一个刷新功能,需要广播到子 Controller 。我在父 Controller 中做了类似的事情: // Refresh/Cancel $scope.OnGridBODRefre
我正在尝试在计算中使用字典值,如下所示: mydict = dict(zip(['key1', 'key2', 'key3'], [1, 2, 3])) print
刚刚掌握使用 MPI 的 Java 接口(interface)进行并行编程。只是想知道是否有人可以非常简单地解释广播的工作原理? 我有以下内容: if (me ==0) { // This is th
我正在处理一个项目,当我发送消息时,我将它作为通知发送给另一个用户使用广播它工作正常但是当我再次发送新消息然后替换为旧通知而不创建新通知 下面是我生成通知的代码 NotificationCompat.
我是 android 的初学者。但我非常需要你的帮助。我有一个流媒体视频广播视频项目。我找不到好的示例,在哪里可以实现从摄像机录制视频、将流发送(上传)到服务器以及从服务器下载(获取流)到播放器。请帮
请帮我解决我的问题。当我从父 Controller 调用并在子 Controller 中捕获时,为什么 $broadcast 函数不起作用?
我如何从 shell 中看到设置了哪些套接字选项?我特别想知道是否设置了 SO_BROADCAST? 最佳答案 你看过lsof了吗? 关于linux - 广播 socket ,我们在Stack Ove
当我在 Numpy 中进行此操作时会发生什么? a = np.ones([500,1]) b = np.ones([5000,])/2 c = a + b # a.shape (500,1) # b.
我有一个 Nexus S,当我在手机上手动更改日期时,并不总是广播 ACTION_DATE_CHANGED。如果我将日期从 2014 年 2 月 13 日更改为 2014 年 2 月 14 日,我还没
环境:springboot2.3.9RELEASE + RocketMQ4.8.0 依赖 <dependency>  
UDP 广播 面向连接的传输(如 TCP)管理两个网络端点之间的连接的建立,在连接的生命周期的有序和可靠的消息传输,以及最后,连接的有序终止。相比之下,类似 UDP 的无连接协议中则没有持久化连接的概
我正在开发一个带有 Angular 的单页应用程序,我需要在两个不同的指令之间进行通信,这些指令基本上没有父子关系。 在指令 A 中,我有 2 个地方需要从不同的功能广播相同的事件。在指令 B 中,为
我有一个带有多个重复项的主要二维 numpy 数组和一个具有第一个唯一值的辅助数组。 [[ 0 0 1 ] [ 1 0 2 ] [ 2 0 2 ] ... [ 0 0 1 ]
我正在制作多人网络游戏。现在要连接到服务器,客户端需要服务器的 ip 地址。 所以,我的实现方式如下。 客户端在广播 IP 和端口 A 上广播其 IP 地址。服务器通过 A 监听它,并且 服务器与客户
是否可以在没有 Urban Airship 等服务的情况下广播推送通知? 谢谢。 最佳答案 当然可以,但是您需要自己实现整个基础架构。 http://developer.apple.com/libra
我想复制矩阵的每一行 M没有任何复制发生(即通过创建 View ): 0 1 0 1 2 3 -> 0 1 2 3
我从一个 2D 数组开始,想将它广播到一个 3D 数组(例如,从灰度图像到 rgb 图像)。这是我使用的代码。 >>> img_grey = np.random.randn(4, 4) >>> img
我是一名优秀的程序员,十分优秀!