android服务和活动,Android服务和活动之间的通信

cce1a62eb6876bf777b4c053f6514fdd.png

我想开发一个包含三个活动和两个服务的Android应用程序.

名为WebClientService的第一个Service使用Handler每30秒调用一次REST API,并且必须通过结果通知活动的Activity.

它还必须通知名为DatabaseService的第二个服务,以便更新本地数据库.

数据库服务只会在活动的创建时调用一次(在应用程序崩溃和重新启动的情况下),并且只在onRestart调用一次(这样我们就可以在出现连接问题时显示数据).由于WebClientService每隔30秒通知“活动”活动,因此活动将自动更新.

问题是:

>通知更新活动活动和后台DatabaseService的最佳方法是什么?

我的想法是在WebClientService中使用sendBroadcast(),在每个活动和DatabaseService中使用BroadcastReceiver,这是正确的方法吗?

>我应该使用相同的方法进行AllMeetingRoomActivity和DatabaseService之间的通信,还是应该使用Bound Service?

谢谢

更新:

DatabaseService不再是后台服务,而只是WebClientService和活动之间的db层的共享实例.

现在的问题是:这是一个很好的方法,只需将我的30秒更新写入本地数据库,并允许活动每隔几秒钟更新一次,只需从本地数据库读取?

这会对性能产生太大影响吗?

语境:

遵循我到目前为止所实现的,但使用SettableFutures,因此一旦我明确了如何使它们有效地进行通信,就需要使用服务和广播重新实现:

public class MainActivity extends AppCompatActivity {

private TextView meetingsTextView;

private EditText mEdit, editSubject;

private final ConnectorInitializer clientInitializer = new ConnectorInitializer();

private AppConnector genericClient; // can use OutlookClient or a test client to talk with a mock server

@Override

protected void onCreate(Bundle savedInstanceState) {

// initializes client based on the settings in "config.json"

genericClient = clientInitializer.create(this);

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

meetingsTextView = (TextView) findViewById(R.id.NowMeeting);

mEdit = (EditText)findViewById(R.id.editText);

editSubject = (EditText)findViewById(R.id.editSubject);

Futures.addCallback(genericClient.logon(this, scopes), new FutureCallback() {

@Override

public void onSuccess(Boolean result) {

Log.d("APP", "-- Logged in. --");

databaseConnector.synchronouslyGetBackupFromLocalDatabase() // FUTURE

// callback here

// onSuccess, onFailure

}

@Override

public void onFailure(@NonNull Throwable t) {

Log.e("\n ~~~~>> logon \n", t.getMessage());

meetingsTextView.setText(R.string.Login_Failed);

}

});

}

/** At the moment the UI is not updated automatically every 30 seconds

* but manually using a refresh button

*/

public void getBookings(@SuppressWarnings("UnusedParameters") View view){

Log.d("APP", "Retrieve button clicked: "+(DateTime.now())+". Calling async getCalendar.");

meetingsTextView.setText(R.string.retrieving_events);

try{

Futures.addCallback( genericClient.getCalendarEvents(), new FutureCallback(){

@Override

public void onSuccess(final String resultCalendars) {

Log.d("APP", "Success. Result: "+resultCalendars);

runOnUiThread(new Runnable() {

@Override

public void run() {

Log.d("APP", "Calendars SUCCESSFULLY retrieved.");

String meetingsRetrieved = getString(R.string.calendar)+resultCalendars;

meetingsTextView.setText(meetingsRetrieved);

Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();

}

});

databaseConnector.asyncUpdateLocalDbWithResults(); // FUTURE

// callback here

// onSuccess, onFailure

}

@Override

public void onFailure(@NonNull Throwable t) {

Log.e( "APP", "Calendar error. Cause: "+t.getLocalizedMessage() );

String retrieveError = "Retrieve error. \n\n\n"+t.getLocalizedMessage();

meetingsTextView.setText(retrieveError);

Toast.makeText(getApplicationContext(), "Fail!", Toast.LENGTH_LONG).show();

}

});

}catch(Exception ex){

Log.e("APP","Something went wrong in your code. Cause:"+ex);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值