【问题标题】:How to pass context from activity to service如何将上下文从活动传递到服务
【发布时间】:2019-09-04 20:46:10
【问题描述】:

我有一个问题,我需要从服务访问活动中的视图。我认为这样做的一个好方法是传递活动上下文,然后使用它来访问视图。然而,当我运行它时,它会抛出一个空错误,这意味着对象(视图)为空。这是代码,我想知道如何解决这个问题:

public class Purchase extends AppCompatActivity {
   private TextView purchaseAmount;
   private Button but;

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_purchase);
      but = (Button) findViewById(R.id.makePurBut);
      purchaseAmount = (TextView) findViewById(R.id.purchaseAmout);
      Intent i = new Intent(Purchase.this, MyHostApduService.class);
      MyHostApduService myHostApduService = new MyHostApduService(Purchase.this);
      startService(i);
  }
}


public class MyHostApduService extends HostApduService {
static Context context;
   public MyHostApduService(Context context)
   {
      this.context = context;
   }

   public int onStartCommand(Intent intent, int flags, int startId) {
      textView = (TextView)((Activity)context).findViewById(R.id.purchaseAmout);
      but = (Button)((Activity)context).findViewById(R.id.makePurBut);
      return flags;
  }
}

【问题讨论】:

标签: android android-activity android-service android-context


【解决方案1】:

Service 有自己的上下文,不能直接与 UI 交互。

但您可以创建绑定服务或使用ResultReceiver。例如,此处描述的选项:https://medium.com/@ankit_aggarwal/ways-to-communicate-between-activity-and-service-6a8f07275297

例如创建绑定服务,如示例所示:

  1. 添加监听接口:
interface Callback {
  void sendMes(Object payload); // or more specific signature
}
  1. 在役:
    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        List<Callback> listeners;

        LocalService getService() {
            return LocalService.this;
        }
        void addListener(Callback listener) {
             listeners.add(listener);
        }
        void removeListener(Callback listener) {
             listeners.remove(listener);
        }
    } 
  1. 在活动中(实现Callback):
private LocalService mBoundService;
private LocalService.LocalBinder mBinder;

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        mBinder = ((LocalService.LocalBinder)service);
        mBoundService = ((LocalService.LocalBinder)service).getService();
        ((LocalService.LocalBinder).addListener(MyActivity.this);
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
    } 
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    bindService(new Intent(MyActivity.this, LocalService.class),
                mConnection,
                Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

@Overrride 
public void sendMes(Object payload) { ... }

@Override
protected void onDestroy() {  
 super.onDestroy(); 
 if (mIsBound) {
        mBinder.removeListener(this);
        unbindService(mConnection);
        mIsBound = false;
    }
}

【讨论】:

    【解决方案2】:

    解决您的问题的最简单方法是use LocalBroadcastManager

    从 Service 向 Activity 发送广播,然后在 onReceive 中您可以在 Activity 本身中操作视图。

    确保在 onStart/onPause 中注册/取消注册 Receiver,而不是像内部链接那样在 onCreate/onDestroy 中注册。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      相关资源
      最近更新 更多