请在回答前看编辑内容!
我有一个包含BackgroundService类的应用程序:
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>
所以基本上我的目标是在我的应用程序打开时初始化我的BackgroundService,并让它继续在后台运行,做我需要做的任何事情。截至目前,我正在使用日志来确定我的“设置”是否正常工作,但是当我运行我的应用程序时,即使在我测试了应该触发我的BroadCastReceiver的所有操作之后,我也看不到日志。此外,如果我的服务正在运行,我的持续通知应该已经更改,但它不会......
修改:
因此,我将日志添加到我的BackgroundService的onCreate()和onReceive()方法中,但是,似乎都没有出现。我想知道,我是否需要在我的启动器活动中做一些事情来初始化服务?此外,没有显示任何通知,所以我认为服务由于某种原因没有启动...
最新修改:
所以我将以下代码添加到我的Main活动中,看看它是否会产生影响:
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
指向我的BroadCast接收器类。
答案 0 :(得分:1)
Intent服务专为简短任务而设计。你的意图处理方法是空的。
如果您在后台需要长时间运行任务,请使用标准服务并调用start foreground。这将最大限度地减少系统破坏您服务的可能性。
要了解详情,请转到here
修改
尝试覆盖onStartCommand方法。启动服务时调用此方法,通常在此处执行所有操作。请记住,有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"));