我目前正在开发一个小应用程序,以开始使用蓝牙Android API提供的服务.
编辑 - >答案:
似乎问题是由于特定的Nexus 5设备.好像他们的蓝牙接收器不能正常工作.以下解决方案适用于其他设备
备注:
我在这里阅读了文档:http://developer.android.com/guide/topics/connectivity/bluetooth.html 以及本教程的以下源代码http://www.londatiga.net/it/programming/android/how-to-programmatically-scan-or-discover-android-bluetooth-device /位于/ lorensiuswlt/AndroBluetooth下的github上
我已经完成了几乎所有感兴趣的功能(例如检查适配器是否存在,启用/禁用蓝牙,查询配对的divices,设置适配器是否可发现).
问题:
实际上,当我启动.onDiscovery()方法时,找不到任何设备,即使在我的Nexus 5上的设置/蓝牙中找到了设备.
我是如何处理的:
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
...
protected void onCreate(Bundle savedInstanceState) {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
}
Run Code Online (Sandbox Code Playgroud)
该过滤器工作良好,据我可以尝试,即ACTION_STATE_CHANGED(蓝牙启用)和两个ACTION_DISCOVERY _***.
然后成功地调用以下方法:
public void onDiscovery(View view)
{
mBluetoothAdapter.startDiscovery();
}
Run Code Online (Sandbox Code Playgroud)
然后我有我的蓝牙接收器:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) …
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,您可以下载一些文件 我使用Android DownloadManager
类进行下载.下载完成后,它会显示一条消息,说明该文件已下载.问题是,可能同时有2,3或4次下载.我的BroadcastReceiver
代码看起来像这样:
receiver_complete = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ){
Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.download_finished, "Here should be the name", Toast.LENGTH_SHORT).show();
}
}
};
Run Code Online (Sandbox Code Playgroud)
如何获取完成下载的当前文件名?
非常感谢你.
java android broadcastreceiver receiver android-download-manager
我在数据库中有一系列测试和案例.每当测试被废弃时,它都会过时,并且该测试的任何子案例也应该过期.我认为有两种方法可以做到这一点:
1)将保存功能修改为结束日期子案例.
2)创建一个侦听正在保存的测试模型的接收器,然后结束其子案例的日期.
有没有理由使用另一个以外的人?
编辑:我看到这篇博文建议在检查模型的给定值时使用save方法.因为我正在检查end_date,也许这表明我应该使用自定义保存?
Edit2:另外,对于记录,完整的层次结构是Protocol - > Test - > Case - > Planned_Execution,并且只要一个end_dated,每个子节点也必须是endDated.我想我最终会为每个人做同样的事情.
编辑3:事实证明,为了判断当前的save()是否是结束测试的那个,我需要访问旧数据和新数据,所以我使用了自定义保存.这是它的样子:
def save(self):
"""Use a custom save to end date any subCases"""
try:
orig = Test.objects.get(id=self.id)
enddated = (not orig.end_date) and self.end_date is not None
except:
enddated = False
super(Test, self).save()
if enddated:
for case in self.case_set.exclude(end_date__isnull=False):
case.end_date = self.end_date
case.enddater = self.enddater
case.save()
Run Code Online (Sandbox Code Playgroud) 我正在用一个定义带注释的接收器类型的方法来查看一个相当简单的类:
class Foo<T> {
void foo(Foo<@Bar T> this) {}
}
Run Code Online (Sandbox Code Playgroud)
我现在想访问接收器类型参数的类型注释,@Bar
但Java反射API在访问接收器时返回带注释的原始类型:
assert Foo.class.getDeclaredMethod("foo")
.getAnnotatedReceiverType()
instanceof AnnotatedParameterizedType;
Run Code Online (Sandbox Code Playgroud)
断言失败,因为返回的带注释的类型作为原始类型返回Foo
.这是故意的吗?@Bar
当访问AnnotatedType
返回的实现的私有属性时,我仍然可以找到注释.
我正在运行最新版本的Java 8.
为了创建一个接收器,我WakefulBroadcastReceiver
在我的旧项目中进行了扩展.但现在是deprecated
.而不是WakefulBroadcastReceiver
它Receiver
,我应该现在使用以及如何将下面的代码转换的新方法?
这是我的代码:
public class TaskFinishReceiver extends WakefulBroadcastReceiver {
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
@Override
public void onReceive(Context context, Intent intent) {
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
turnOnScreen();
Intent wakeIntent = new Intent();
wakeIntent.setClassName("com.packagename", "com.packagename.activity.TaskFinished");
wakeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(wakeIntent);
}
public void turnOnScreen(){
mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
mWakeLock.acquire();
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在学习如何在android上创建和使用服务.我看了android SDK以获得进一步的帮助,我发现了android:enable = [true | 错误的].
在SDK中说:
和属性必须都是"true"(因为它们都是默认值)才能启用服务.如果其中任何一个为"假",则禁用该服务; 它无法实例化.
所以我想知道/为什么(一般情况下)的兴趣是什么?
我说如果我们把服务启用设为false就无法调用该服务,那么我们为什么要首先创建该服务呢?
谢谢,抱歉这么长的消息.
我有一个使用android.support.v4.os.ResultReceiver传递数据的IntentService.在IntentService中,当我使用ResultReceiver.send方法发回结果时,Android Studio显示错误说明
ResultReceiver.send只能从同一个库组中调用(groupId = com.android.support)
我在调用ResultReceiver的构造函数时收到类似的消息.但即使出现此错误,代码也会运行.在我将支持库版本升级到25.1.1后,这开始出现.
我该怎么办才能解决这个问题?
我为Android编写了一个小部件,我试图获得电池电量.我试过用了
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Run Code Online (Sandbox Code Playgroud)
但我得到错误:"不允许IntentReceiver组件注册接收意图"
为什么?ACTION_BATTERY_CHANGED是一个粘性意图,我没有注册接收者(在第一个参数中为null).
任何解决方法?
谢谢.
在我的应用程序有时我收到此错误:
java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@4806a4a8
at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:667)
at android.app.ApplicationContext.unregisterReceiver(ApplicationContext.java:840)
at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:321)
at android.widget.ViewFlipper.onDetachedFromWindow(ViewFlipper.java:104)
at android.view.View.dispatchDetachedFromWindow(View.java:5891)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1076)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1570)
at android.view.ViewRoot.doDie(ViewRoot.java:2565)
at android.view.ViewRoot.die(ViewRoot.java:2535)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:218)
at android.view.Window$LocalWindowManager.removeViewImmediate(Window.java:436)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3498)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3599)
at android.app.ActivityThread.access$2300(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1867)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
这是什么?我该怎么办?需要一些帮助,请...
这是一些代码:
在onCreate()
方法我有这样的:
vf = (ViewFlipper) findViewById(R.id.details);
vf.setFlipInterval(3000);
vf.startFlipping();
populate();
Run Code Online (Sandbox Code Playgroud)
其中populate方法是这样的:
private void populate() {
for (int i …
Run Code Online (Sandbox Code Playgroud) 我有一个带有GCM推送通知的应用程序.通知从CMS发送.
我的appTargetSdkVersion = 27.
一切正常,除了两个事实:
应用程序在Android ver低于Oreo的设备上被杀死时不会收到通知.
在奥利奥我必须打开设备屏幕才能接收通知.
我不知道为什么它会像那样.
*在BroadcastReceiver的onReceive方法中,我将enqueueWork和启动JobIntentService onHandleWork方法发布通知.
*在我使用WakefulBroadcastReceiver + startWakefulService + IntentService之前,它运行良好.但是我改变了这个实现,因为在将targetSdk升级到27个通知后停止在Oreo上工作(我知道它是由后台服务限制引起的).
编辑:
我通过将通知通道参数IMPORTANCE从IMPORTANCE_DEFAULT更改为IMPORTANCE_HIGH来修复此问题!
我的GcmReceiver类:
public class GcmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GcmNotificationService.class.getName());
GcmNotificationService.enqueueWork(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
Run Code Online (Sandbox Code Playgroud)
}
我的JobIntentService类:
public class GcmNotificationService extends JobIntentService {
private static final String TAG = "GcmNotificationService";
private Random random = new Random();
static final int JOB_ID = 1000;
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, GcmNotificationService.class, JOB_ID, …
Run Code Online (Sandbox Code Playgroud) service notifications android receiver google-cloud-messaging