- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在试验 requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)
和 BroadcastReceiver。代码如下:
// PendingLocationDemo.java
public class PendingLocationDemo extends Activity {
public TextView output;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView) findViewById(R.id.output);
SomeReceiver receiver = new SomeReceiver(this);
IntentFilter filter = new IntentFilter();
filter.addAction("some_action");
// filter.addAction("some_other_action");
// filter.addAction("still_something_different");
filter.addCategory(Intent.CATEGORY_DEFAULT);
getApplicationContext().registerReceiver(receiver, filter);
Intent intent = new Intent();
intent.setAction("some_action");
PendingIntent pending = PendingIntent.getBroadcast(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// Get the location manager
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 3000, 0, pending);
}
protected void someCallback(Intent intent) {
printObject(intent);
}
}
// SomeReceiver.java
public class SomeReceiver extends BroadcastReceiver {
PendingLocationDemo caller;
public SomeReceiver(PendingLocationDemo caller) {
this.caller = caller;
}
@Override
public void onReceive(Context context, Intent intent) {
caller.someCallback(intent);
}
}
当我在设备上运行它时,输出取决于我在 IntentFilter 中用于操作的值。
对于这种不一致的行为是否有合理的解释?
最佳答案
这种奇怪的行为是由于广播接收器的注册。您可以在位置监听器中编写代码,而不是注册广播接收器,它也是一个广播接收器并在后台监听位置更新。
final LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Code for action you wish to perform on Location Changed.
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// ...
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
3000, locationListener);
希望对您有所帮助。
关于android - 设备上 IntentFilter 分辨率的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5109663/
什么是使用在广播接收器(而不是 android.xml)中的代码中声明的 Android IntentFilters 过滤掉特定 cmp 字符串的合理方法? 我要搜索的案例有: action =
当 ListView fragment 或其他(嵌套) fragment 中发生某些事情时,我想刷新 ListView(自定义类)中的特定行。 我目前正在使用这种模式(News 是 NewsView
我有一个问题。我需要中断不是静态的链接,但用户将在运行时创建所有列表。所以我在运行时创建我的 intentfilter 对象。因此,当我在 list 文件中使用代码时,它可以工作。但是在 java 类
我不太明白 IntentFilter 的作用是什么?这是一个示例 list : 是什么意思?和 标签呢?假设我声明:
因此,我正在尝试制作一个应用程序,在收到每条短信时都会显示消息提示。我希望应用程序只显示 toast ,我目前使用这段代码来显示它,但在应用程序运行时出现异常错误。我正在使用这段代码 Broadcas
这就是我将我的应用程序 Intent 过滤器的优先级设置为最高的方式。在收到 Intent Extras 后我中止了广播,但 native 应用程序仍在接收短信并且通知栏仍在显示最烦人的消息内容因为我
我正在试验 requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)
我想接听来电。 我知道您必须在 AndroidManifest 中授予权限并设置接收器 和 ... 但是,在我的项目中,我通过代码设置了接收器, IntentFilter filter = new
BroadcastReceiver 是否允许应用从实时媒体源接收视频流? 最佳答案 BroadcastReceiver 是一个 Android 组件,它允许您注册系统或应用程序事件。事件发生后,And
我正在开发一个 Android 应用程序。我必须将一个 Activity 转换为 fragment 。但是 Activity 是在带有一些 Intent 过滤器的 list 文件中注册的。如何使用 I
我使用隐式电子邮件 Intent 创建了一个电子邮件应用程序: 我知道我需要在 list 文件中添加 Filter 。它适用于这些过滤器。 我的问题是,为什
我有一个像这样的可序列化对象: public class Main implements Serializable { private static final long serialVers
上下文是:Framework 4.5,Xamarin.Android v5.0 我想使用 NFC 技术为我的应用程序用户实现快捷方式。我希望用户扫描 NFC 标签,这样他们只需为预定义方案赋值即可。
我创建了一个包含广播接收器实现的 Android 类库: [IntentFilter(new string[] { "com.google.android.gcm.intent.RETRY" }, C
我正在尝试创建一个每分钟更新一次的时钟小部件。 我尝试使用 AlarmManager 发送更新请求,但我认为这不如使用 IntentFilter(Intent.ACTION_TIME_TICK) 准确
我正在浏览器中进行用户身份验证。 成功通过身份验证后,浏览器将关闭并执行 AndroidMainActivity.OnCreate()。但是应用程序显示空白屏幕,就像没有加载 View /页面一样。我
我是 Monodroid 的新手,想知道是否有办法使用 IntentFilter 属性来声明它: 我尝试使用 Visual Studio 的智能感知查找它,但没有显示
嗨。我是一个 Android 初学者,试图制作一个可以过滤多个操作的 IntentFilter。不幸的是,当我开始使用 addAction 方法时,Eclipse 抛出一个错误: "Syntax er
是否可以在 android 中创建一个 IntentFilter 来匹配手机上广播的所有 Intent (可能通过使用 BroadcastReceiver)? IE。我使用手机时在 ddms 中看到的
预期行为:应用应打开网站进行登录 实际行为:遇到错误您必须将 WebAuthenticatorCallbackActivity 子类化并为其创建一个与您的 callbackUrl 匹配的 Intent
我是一名优秀的程序员,十分优秀!