- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 addProximityAlert:
Intent intent = new Intent(getString(R.string.intent_message_location_fetched));
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
locationManager.addProximityAlert(LAT, LONG, RADIUS, 0, proximityIntent);
据我所知,R.string.intent_message_location_fetched 应该在位置接近 LAT LONG 之后触发。(R.string.intent_message_location_fetched = com.myapp.client.MyActivity.LocationFetched)
然后我尝试创建 BroadcastReceiver 类:
public class MyBroadcastReciver extends BroadcastReceiver {
MyActivity mainActivity;
public MyBroadcastReciver(MyActivity activity)
{
mainActivity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
mainActivity.ShowToast("Recived : " + intent.getAction());
}
}
并在 MyActivity 类中注册接收者:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myReciver = new MyBroadcastReciver(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(getString(R.string.intent_message_location_fetched));
registerReceiver(myReciver, intentFilter);
...
但是当我在 LAT LONG 位置时没有任何反应。怎么了?我试过在 list 中注册 Receiver,但没有用。
附言当我使用 getActivity 而不是 getBroadcast 时,它工作正常并恢复了我的 Activity(如果它已经死了)。
最佳答案
好的,伙计们。无论如何。现在我明白了 Intents 是如何工作的。最主要的是(我想是)我使用了不同的上下文。
现在我有了自己的类,其中包含使用 PendingIntent 调用的 addProximityAlert 函数。我传递了 WrapperContext activityContext 而不是 this(当我创建我的类时,我传递了我的 Activity 的范例):
//Prepare proximity Intent
proximityIntent = new Intent(activityContext.getString(R.string.intent_message_location_fetched));
proximityPendingIntent = PendingIntent.getBroadcast(activityContext, 0, proximityIntent, 0);
locationManager.addProximityAlert(latitude, longitude, (float) radius, -1, proximityPendingIntent);
注意过期值 = -1!否则您的 Intent 可能在您捕捉到它之前就已经死了。
第二件事 - 我在 list 中使用 action="@strings/my_message"注册了 intent-filter:
<activity ...>
...
<intent-filter>
<action android:name="@string/intent_message_location_fetched"/>
</intent-filter>
</activity>
然后我在自己的类构造函数中有这个:
//Create and register receiver
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(context.getString(R.string.intent_message_location_fetched))
&& intent.getExtras().getBoolean(LocationManager.KEY_PROXIMITY_ENTERING))
{
Toast.makeText(activityContext, "LOCATION INTENT WORKS!", Toast.LENGTH_SHORT).show();
}
}
};
activityContext.registerReceiver(mReceiver, new IntentFilter(activityContext.getString(R.string.intent_message_location_fetched)));
我希望这对某人有用。
关于android - IntentReceiver 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6612716/
我的 onCreate() 中有以下代码: registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_
我在特定 Activity 中通过单击按钮打开发送菜单: Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); i.p
我使用 addProximityAlert: Intent intent = new Intent(getString(R.string.intent_message_location_fetched
在我的 Android 应用程序中,我有一个 Activity MainActivity,它有一个 Fragment MainFragment,它启动一个 IntentService,然后转换到 Ac
E/ActivityThread( 655): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver
我正在编写非常简单的应用程序。它工作正常,但是当我按下后退键时它崩溃了(Activity 泄露了最初在此处注册的 IntentReceiver)。这个应用程序应该在后台运行。如何正确执行? impor
我正在尝试一起发送短信和邮件。发送邮件没有问题,但是当我发送短信时收到此异常: End has leaked IntentReceiver Are you missing a call to unre
程序员。我正在做一个 Proximity Alert App,想知道如何从 ProximityIntentReceiver extends BroadcastReceiver 发送 SMS,即在进入特
在我的 Widget 中,我使用: Intent intent = new Intent(context, ClassForWidget.class); PendingIntent pendingIn
场景如下,我有一个 LoginActivity,它使用 WifiManager 来获取 IP 地址,如下所示: WifiManager wifiManager = (WifiManager)getSy
在 list 中,我只有 Activity ,接收器上没有任何服务。当我完成 Activity 时,我在 logcat 中看到了这个: E/ActivityThread: Activity com.e
mapView = (MapView) findViewById(R.id.mapview); mapView.onCreate(savedInstanceState); mapVie
我正在构建一个需要 IntentReceiver 的小应用。 其实我是Android开发新手,iOS开发后正在做switch。我有以下代码: package com.TheIntent; import
我正在制作一个安卓应用。我正在制作一项服务,该服务在用户选中 checkbox 时一直在后台运行,当用户 unckeck 时它应该停止。所以当我选中该框并且它还显示“服务启动” toast 时它确实有
我希望能在这里找到一些帮助,因为我不熟悉 Android 中的 BroadcastReceivers。这段代码打开一个 WebView,将您重定向到登录页面,并在检测到 URL 更改后接收登录 tok
在将此帖子标记为已关闭或重复之前,我想说的是,我已经完成了类似帖子中提到的所有事情,但都没有奏效。 我有 2 个接收器,我从中获取数据。所以我想在 Activity 开始时注册接收器,并在 Activ
我的 Android 应用程序中有一个 MapActivity,它使用 osmdroid(Open Street Map for Android 库)显示 map 。 当我在此 MapActivity
我正在使用前台 Android Service 通过 REST API 通过 Retrofit 请求资源,作为每 60 秒的计时器。此类服务也在与应用程序不同的进程中运行,这是必要的,这样 Andro
我希望我的应用程序在用户触摸 GLSurfaceView 时在 GLSurfaceView 上显示 ZoomButtonsController。我的 Activity 构造函数如下所示: _zoomB
IntentReceiver 正在泄漏 由于 onDetachedFromWindow 在某些情况下未被调用。 @Override protected void onDetachedFromWind
我是一名优秀的程序员,十分优秀!