3

我目前在 viewPager 中有 3 个片段。其中一个片段是注册 aBroadcastReceiver但在当前的实现中我什么也没得到。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    getSherlockActivity().getApplicationContext().registerReceiver(
        broadcastReceiver,
        new IntentFilter(UpdateService.UPDATE_NOTIFICATION));
    optionsBroadcast = true;
}

whereoptionsBroadcastboolean用于跟踪被调用服务的值broadcastReceiver,如下所示:

public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("onBroadcastReceiver", "service is being called ?");
        updateOptionsMenu(intent);
    }
};

onPause我还在and中实现了 unRegister 逻辑onResume

现在在服务类中:

public class UpdateService extends Service{

    Intent updateIntent;
    public static final String UPDATE_NOTIFICATION =  "com.eps.support.updatenotification";

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        updateIntent = new Intent(UPDATE_NOTIFICATION);
        new CheckUpdates().execute(getApplicationContext());
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        Log.i("inside service", "onStartCommand");
        return START_STICKY;
    }


    public class CheckUpdates extends AsyncTask<Context, Void, String>{

        //URL parsing logic where either the setOnEqual() or setOnUpdate() is called.
    }

    public void setOnEqual(){
        updateIntent.putExtra("isUpdated", 1);
        sendBroadcast(updateIntent);

    }

    public void setOnUpdate(){
        updateIntent.putExtra("isUpdated", 2);
        sendBroadcast(updateIntent);
    }
}

我还更新了将类作为服务的清单文件。由于某种原因,该服务无法正常工作。或者它没有做它应该做的事情。setOnEqual()

一旦我们注册了服务,服务就会调用AsyncTask其中之一setOnEqual()或被setOnUpdate()调用的位置。Log在服务中设置了所有条目后,它们都不会被调用。

请帮忙。

4

0 回答 0