如何从其他活动中调用服务方法

时间:2013-06-04 09:02:10

标签: android service methods call

我有问题如何在服务中调用我的方法,我的方法是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的结果。

1 个答案:

答案 0 :(得分:1)

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