包含 BroadCastReceiver 的服务无法正常运行
Service containing BroadCastReceiver not functioning correctly
Please see edits before answering!
我有一个包含 BackgroundService 的应用程序 class:
public class BackgroundService extends Service {
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction("com.spotify.music.playbackstatechanged");
filter.addAction("com.spotify.music.metadatachanged");
filter.addAction("com.spotify.music.queuechanged");
registerReceiver(receiver, filter);
Log.e("Playing:", "APP IS PLAYING");
Notification notification = new Notification();
startForeground(1, notification);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long timeSentInMs = intent.getLongExtra("timeSent", 0L);
String action = intent.getAction();
if (action.equals(BroadcastTypes.METADATA_CHANGED)) {
String trackId = intent.getStringExtra("id");
String artistName = intent.getStringExtra("artist");
String albumName = intent.getStringExtra("album");
String trackName = intent.getStringExtra("track");
int trackLengthInSec = intent.getIntExtra("length", 0);
// Do something with extracted information...
} else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {
boolean playing = intent.getBooleanExtra("playing", false);
Log.e("Playing:","TRUE");
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
static final class BroadcastTypes {
static final String SPOTIFY_PACKAGE = "com.spotify.music";
static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";
static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
}
}
这是在我的清单中声明的:
<service
android:name=".BackgroundService"
android:enabled="true" >
<intent-filter>
<action android:name="com.spotify.music.playbackstatechanged" />
<action android:name="com.spotify.music.metadatachanged" />
<action android:name="com.spotify.music.queuechanged" />
</intent-filter>
</service>
所以基本上我的 objective 是在我的应用程序打开时初始化我的 BackgroundService,并让它继续 运行 在后台做我需要它做的任何事情。到目前为止,我正在使用日志来确定我的 "setup" 是否正常工作,但是当我 运行 我的应用程序时,即使在我测试了所有应该触发我的 BroadCastReceiver 的操作之后我也看不到日志.此外,如果我的服务是 运行ning,我的持续通知应该会改变,但它不会...
Edit::
因此,我将日志添加到我的 BackgroundService 的 onCreate() 和 onReceive() 方法中,但是,这两个方法似乎都没有出现。我想知道,我需要在我的启动器 activity 中做些什么来初始化服务吗?此外,没有显示任何通知,所以我假设该服务由于某种原因没有启动...
Latest Edit:
所以我将以下代码添加到我的 Main activity 中,看看它是否会有所不同:
startService(new Intent(this,BackgroundService.class));
调试我的应用程序后,我开始看到以下错误:
java.lang.RuntimeException: Unable to create service com.aurum.mutify.BackgroundService: java.lang.SecurityException: Isolated process not allowed to call registerReceiver
指向我的广播接收器class。
意图服务专为短任务而设计。而你的意图处理方法是空的。
如果您需要在后台执行较长的 运行 任务,请使用标准服务并调用启动前台。这将最大限度地减少系统破坏您的服务的可能性。
要了解更多信息,请前往 here
编辑
尝试覆盖 onStartCommand 方法。这个方法在服务启动时被调用,通常你在这里做所有的事情。请记住 return 有 3 个选项。
编辑 2:
尝试这样的事情
在创建
PendingIntent pi;
BroadcastReceiver br;
Intent myIntent;
@Override
public void onCreate()
{
super.onCreate();
myIntent = new Intent("something")
if(Build.Version.SDK_INT >= 16) //The flag we used here was only added at API 16
myIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
//use myIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); if you want to add more than one flag to this intent;
pi = PendingIntent.getBroadcast(context, 1, myIntent, 0);
br = new BroadcastReceiver ()
{
public void onReceive (Context context, Intent i) {
new thread(new Runnable()
{
public void run()
{
//do something
}
}).start();
}
};
然后在启动命令中
this.registerReceiver(br, new IntentFilter("something"));
Please see edits before answering!
我有一个包含 BackgroundService 的应用程序 class:
public class BackgroundService extends Service {
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction("com.spotify.music.playbackstatechanged");
filter.addAction("com.spotify.music.metadatachanged");
filter.addAction("com.spotify.music.queuechanged");
registerReceiver(receiver, filter);
Log.e("Playing:", "APP IS PLAYING");
Notification notification = new Notification();
startForeground(1, notification);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long timeSentInMs = intent.getLongExtra("timeSent", 0L);
String action = intent.getAction();
if (action.equals(BroadcastTypes.METADATA_CHANGED)) {
String trackId = intent.getStringExtra("id");
String artistName = intent.getStringExtra("artist");
String albumName = intent.getStringExtra("album");
String trackName = intent.getStringExtra("track");
int trackLengthInSec = intent.getIntExtra("length", 0);
// Do something with extracted information...
} else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {
boolean playing = intent.getBooleanExtra("playing", false);
Log.e("Playing:","TRUE");
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
static final class BroadcastTypes {
static final String SPOTIFY_PACKAGE = "com.spotify.music";
static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";
static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
}
}
这是在我的清单中声明的:
<service
android:name=".BackgroundService"
android:enabled="true" >
<intent-filter>
<action android:name="com.spotify.music.playbackstatechanged" />
<action android:name="com.spotify.music.metadatachanged" />
<action android:name="com.spotify.music.queuechanged" />
</intent-filter>
</service>
所以基本上我的 objective 是在我的应用程序打开时初始化我的 BackgroundService,并让它继续 运行 在后台做我需要它做的任何事情。到目前为止,我正在使用日志来确定我的 "setup" 是否正常工作,但是当我 运行 我的应用程序时,即使在我测试了所有应该触发我的 BroadCastReceiver 的操作之后我也看不到日志.此外,如果我的服务是 运行ning,我的持续通知应该会改变,但它不会...
Edit::
因此,我将日志添加到我的 BackgroundService 的 onCreate() 和 onReceive() 方法中,但是,这两个方法似乎都没有出现。我想知道,我需要在我的启动器 activity 中做些什么来初始化服务吗?此外,没有显示任何通知,所以我假设该服务由于某种原因没有启动...
Latest Edit:
所以我将以下代码添加到我的 Main activity 中,看看它是否会有所不同:
startService(new Intent(this,BackgroundService.class));
调试我的应用程序后,我开始看到以下错误:
java.lang.RuntimeException: Unable to create service com.aurum.mutify.BackgroundService: java.lang.SecurityException: Isolated process not allowed to call registerReceiver
指向我的广播接收器class。
意图服务专为短任务而设计。而你的意图处理方法是空的。
如果您需要在后台执行较长的 运行 任务,请使用标准服务并调用启动前台。这将最大限度地减少系统破坏您的服务的可能性。
要了解更多信息,请前往 here
编辑
尝试覆盖 onStartCommand 方法。这个方法在服务启动时被调用,通常你在这里做所有的事情。请记住 return 有 3 个选项。
编辑 2: 尝试这样的事情 在创建
PendingIntent pi;
BroadcastReceiver br;
Intent myIntent;
@Override
public void onCreate()
{
super.onCreate();
myIntent = new Intent("something")
if(Build.Version.SDK_INT >= 16) //The flag we used here was only added at API 16
myIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
//use myIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); if you want to add more than one flag to this intent;
pi = PendingIntent.getBroadcast(context, 1, myIntent, 0);
br = new BroadcastReceiver ()
{
public void onReceive (Context context, Intent i) {
new thread(new Runnable()
{
public void run()
{
//do something
}
}).start();
}
};
然后在启动命令中
this.registerReceiver(br, new IntentFilter("something"));