所以我一直在使用Tasker for Android,并让它在后台运行我的服务。但是我的服务每隔一段时间才收到一次意图。我在同一个配置文件上有另一个任务,它显示一个通知,每次都会弹出。不确定这是Tasker的问题还是app的问题。这是我的服务,它使用广播接收器在后台运行以侦听意图:
package com.controlanything.garageapp;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.Toast;
public class IOService extends Service {
private static final String ACTION ="com.controlanything.garageapp.IO_ACTION";
private BroadcastReceiver bReceiver;
public IOService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate(){
super.onCreate();
System.out.println("Service started.");
final IntentFilter theFilter = new IntentFilter();
theFilter.addAction(ACTION);
this.bReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra("COMMAND").equalsIgnoreCase("ON")){
System.out.println("On Command");
makeToast("ON!");
}else{
System.out.println("Off Command");
makeToast("OFF!");
}
}
};
this.registerReceiver(this.bReceiver, theFilter);
}
@Override
public void onDestroy(){
super.onDestroy();
this.unregisterReceiver(this.bReceiver);
}
private void makeToast(String message){
Toast.makeText(this, message, Toast.LENGTH_LONG);
}
}
任何想法都非常感激。