gpt4 book ai didi

android - 如何从其他 Activity 中调用服务方法

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:15 25 4
gpt4 key购买 nike

如何在服务中调用我的方法有问题,我的方法是getambil_jmlgangguan()
详细代码如下:

public class GetCountDataGangguanService extends Service {

public String JUMLAH_GANGGUAN ="";
public static final String TAG = "MyServiceTag1";

GlobalKoneksi konek_url = new GlobalKoneksi();
GetJmlGangguanFunction jmlGangguanFUnctions = new GetJmlGangguanFunction();

private static String KEY_SUCCESS = "success";
private static String KEY_JUMLAH_DATA = "jumlah";
JSONArray jml_data_json = null;

// UserFunctions userFunctions;
UserFunctions userFunctions = new UserFunctions();
SessionManager session;

@Override
public void onCreate() {
// TODO Auto-generated method stub
mHandlers = new ArrayList<Handler>();

Toast.makeText(this, "GetCountDataGangguan.onCreate()",
Toast.LENGTH_SHORT).show();
//getambil_jmlgangguan();
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "GetCountDataGangguan.onBind()", Toast.LENGTH_SHORT)
.show();
return messenger.getBinder();

}

public class LocalBinder extends Binder {
public GetCountDataGangguanService getServerInstance() {
return GetCountDataGangguanService.this;
}
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub

Log.d("GetCountDataGangguan.onDestroy()", "Sudah di destroy");
Toast.makeText(this, "GetCountDataGangguan.onDestroy()",
Toast.LENGTH_SHORT).show();

super.onDestroy();

}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);

}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "GetCountDataGangguan.onUnbind()",
Toast.LENGTH_SHORT).show();
return super.onUnbind(intent);
}


public String getambil_jmlgangguan(){

session = new SessionManager(getApplicationContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
String unit_id = user.get(SessionManager.KEY_UNITID);
String regu_id = user.get(SessionManager.KEY_REGUID);

JSONObject jsondatagangguan = jmlGangguanFUnctions.getcountdata(
regu_id, unit_id);

try {
Log.d("JUMLAH_GANGGUAN",jsondatagangguan.getString(KEY_JUMLAH_DATA));
JUMLAH_GANGGUAN = jsondatagangguan.getString(KEY_JUMLAH_DATA);

} catch (JSONException e) {
e.printStackTrace();
}

return JUMLAH_GANGGUAN;


}

}

如何在另一个活动(如 getambil_jmlgangguan()中调用方法 MainActivity,并定期获得 JUMLAH_GANGGUAN的结果。

最佳答案

我建议您查看下面的链接,其中包含如何绑定到服务的示例:http://developer.android.com/guide/components/bound-services.html

关于android - 如何从其他 Activity 中调用服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16913692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com