- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为 Android 4.3 编写了一个程序,其中包含一个主要 Activity 、一个广播接收器和一个服务。该 Activity 在其 onCreate
方法中绑定(bind)到服务。该 Activity 有一个按钮,可以安排在未来 10 秒后发出警报。警报触发 BroadcastReceiver.onReceive
。此方法试图获取 Binder ,但在某些情况下会失败并且 peekService
返回 null
。
具体来说,如果我执行这 4 个测试,我会得到以下日志:
02-05 20:53:29.992: D/ServiceSSCCE.MyService(476): I've been bound.
02-05 20:53:30.179: D/ServiceSSCCE.MainActivity(476): Service connected.
02-05 20:53:43.265: D/ServiceSSCCE.MyReceiver(476): Awesome, let's get this **** done!
02-05 20:53:55.460: D/ServiceSSCCE.MyReceiver(476): Awesome, let's get this **** done!
02-05 20:54:08.531: D/ServiceSSCCE.MyService(764): I've been bound.
02-05 20:54:08.663: D/ServiceSSCCE.MainActivity(764): Service connected.
02-05 20:54:10.890: D/ServiceSSCCE.MyReceiver(764): Awesome, let's get this **** done!
02-05 20:54:23.593: D/ServiceSSCCE.MyReceiver(788): I just received a null binder.
最后一条消息显示测试 (4.) 失败,而之前的消息显示 (1.)、(2.) 和 (3.) 成功。
我知道 this , this和 this答案,以及谷歌在前两页列出的几乎所有相关结果。我尝试了几件事,包括但不限于:
BroadcastReceiver.onReceive
和主 Activity 调用 startService
(尽管我不确定我是否理解 bindService
和 startService
交互)this
VS getApplicationContext
等等)。我对为什么发生这种情况非常感兴趣,而不是对解决方案感兴趣。
package it.damix.examples.servicesscce;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("ServiceSSCCE.MainActivity", "Service disconnected.");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("ServiceSSCCE.MainActivity", "Service connected.");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService(new Intent(this, MyService.class), mConnection, Context.BIND_AUTO_CREATE);
}
public void scheduleAlarm(View view) {
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(this, 101, new Intent(this, MyReceiver.class), 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pi);
}
}
package it.damix.examples.servicesscce;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
IBinder binder = peekService(context, new Intent(context, MyService.class));
if (binder == null)
Log.d("ServiceSSCCE.MyReceiver", "I just received a null binder.");
else
Log.d("ServiceSSCCE.MyReceiver", "Awesome, let's get this **** done!");
}
}
package it.damix.examples.servicesscce;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Messenger;
import android.util.Log;
public class MyService extends Service {
static class MyHandler extends Handler {
}
@Override
public IBinder onBind(Intent arg0) {
Log.d("ServiceSSCCE.MyService", "I've been bound.");
return new Messenger(new MyHandler()).getBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("ServiceSSCCE.MyService", "I've been unbound.");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
Log.d("ServiceSSCCE.MyService", "I've been rebound.");
super.onRebind(intent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.damix.examples.servicesscce"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="it.damix.examples.servicesscce.MyReceiver" android:enabled="true"></receiver>
<service android:name="it.damix.examples.servicesscce.MyService" android:enabled="true"></service>
</application>
</manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="it.damix.examples.servicesscce.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to schedule an alarm..."
android:onClick="scheduleAlarm"/>
</RelativeLayout>
最佳答案
+1 damix911 非常完整的问题陈述和可能的解决步骤。所有代码的发布使我能够轻松地重新创建测试应用程序。
为什么案例 #4 失败的问题的简短答案在于 peekService() 的一些未记录的细节。的行为。在 this post由 Android 框架工程师 Dianne Hackborn 解释说,为了让 peekService() 返回一个 IBinder
,某些组件之前必须绑定(bind)到该服务,导致系统创建一个 IBinder
.那个帖子是我唯一找到描述的获取 IBinder
的附加条件的地方。
在这里,对于#1 到#3 的情况,MainActivity 的一个实例存在并绑定(bind)到服务,创建一个 IBinder
。当接收器运行并调用 peekService()
时,它会获得 IBinder
。对于案例 #4,从最近的任务列表中滑动应用程序会终止整个应用程序进程: Activity 和绑定(bind)服务。当警报随后触发时,为接收者重新创建应用程序进程,但 Activity 没有启动,没有绑定(bind)到服务的请求,也没有创建服务,所以 peekService()
返回空值。
我无法重现 damix911 的解决方案。当我修改服务属性以使其作为 isolatedProcess
运行时,我遇到了安全异常(KitKat 设备)。我质疑启动该服务并使其成为前台服务会如何改变任何事情。启动它并返回默认( super )模式确实会导致服务变得粘滞,以便在滑动后重新创建进程时将重新创建服务。但是仍然没有绑定(bind)到它,所以 peekService()
没有 IBinder
返回。
关于android - 如果我关闭 Activity ,从 BroadcastReceiver.onReceive 调用 peekService 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28353506/
我正在尝试从 Service 获取 IBinder,以便在收到来自系统的通知时与该服务进行通信。从我的 onReceive() 方法我正在调用 peekService() 但我得到一个空引用。 我在这
我正在尝试使用来自 BroadcastReceiver 的 Android Service。 Service 位于与 BroadcastReceiver 不同的应用程序中。我的理解是,执行此操作的正确
我正在使用启动服务 startService(new Intent(this, RelayService.class)); 然后服务使用启动警报 AlarmManager am = (AlarmMan
问题 我为 Android 4.3 编写了一个程序,其中包含一个主要 Activity 、一个广播接收器和一个服务。该 Activity 在其 onCreate 方法中绑定(bind)到服务。该 Ac
我是一名优秀的程序员,十分优秀!