-1

我们正在开发school application其中,我们正在尝试从服务器获取数据。它工作正常。获取数据后,我们正在尝试使用创建服务我的文件传输在后台运行来下载文件。在此期间,如果我的文件传输服务正在运行,我们希望限制我的用户调用服务器。

我们正在使用以下代码,它显示所有服务,但在服务列表中找不到我的服务,但在后台我的文件传输正在运行即使我们在设置中显示 -> 正在运行的应用程序也显示服务正在运行

我的班级看起来像......有人可以建议我一些替代方式吗?

    public class Getdata {

    Intent DownloadServiceIntent;

    // Progress Dialog
    private ProgressDialog mProgressDialog;

    // Alert Dialog Manager
    Alert_Dialog_Manager alert = new Alert_Dialog_Manager();

    // Database_Handler Object
    Database_Handler dbh;
    SQLiteDatabase db;

    public Getdata(Context context,List<NameValuePair> request) {


        mContext = context;
        Request = request;
        DataUrl = mConnection.fun_GetWebserviceURL()+"Gatway.php";

        // get Database
        dbh = new Database_Handler(mContext);
        db = dbh.get_writeabledb();

        boolean checkservice = isServiceRunning(mContext);
        Log.d("boolean Value",""+checkservice); // Return Always false But My Service Is Running In Background
        if(checkservice){

            alert.showAlertDialog(mContext, " Request Alert ","File Download Started In Background \nPlease Try After Sometime!!", false);

        }else{
            // Get Data
            new GetDataService().execute();
        }
    }

    class GetDataService extends AsyncTask<String, String, String> {



        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(mContext);
            mProgressDialog.setTitle("Please Wait For Few Minutes...");
            mProgressDialog.setMessage("Loading Database... ");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();

        }

        @Override
        protected String doInBackground(String... params) {

            //Getting Data From Server

            return null;
        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            mProgressDialog.dismiss();

            // Starting service for Download Files..
            DownloadServiceIntent = new Intent(mContext,FileTransferService.class);
            mContext.startService(DownloadServiceIntent);

        } // GetWebService


    public boolean isServiceRunning(Context mContext){

         boolean isServiceFound = false;

         final ActivityManager activityManager =(ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);        
         List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

         for (int i = 0; i < services.size(); i++)
         {
         Log.d("Service ClassName ","Service" + i + ":" + services.get(i).service.getClassName().toString());
         if (services.get(i).service.getClassName().toString().equals("com.soch.webservice.FileTransferService"))
         {
                     isServiceFound = true;
         }

         }
         return isServiceFound;
     }
}
4

2 回答 2

1
Try this.

private boolean isMyServiceRunning()
{
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager
            .getRunningServices(Integer.MAX_VALUE))
    {
        if (BgTimerService.class.getName().equals(
                service.service.getClassName()))
        {
            return true;
        }
    }
    return false;
}
于 2013-10-15T10:25:54.057 回答
0

另一种方法可以是:

if(startService(someIntent) != null) { 
    Toast.makeText(getBaseContext(), "Service is already running", Toast.LENGTH_SHORT).show();
}else {
    Toast.makeText(getBaseContext(), "There is no service running, starting service..", Toast.LENGTH_SHORT).show();
}

编辑

我建议使用静态布尔变量来检查正在运行的服务的状态。

还要检查这个链接,它肯定会有所帮助

于 2013-10-15T10:21:44.283 回答