android - 服务不停

标签 android service

我正在创建一个运行服务的应用程序,其中一个函数在 5 秒内被重复调用。我可以通过单击一个按钮来启动服务,但在单击另一个按钮时无法停止!

我的代码是:

public class LocationService extends Service implements LocationListener {
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String TAG = "MyServiceTag";
    Timer t;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        t = new Timer();
        t.scheduleAtFixedRate(
                new TimerTask()
                {
                    public void run()
                    {
                        startJob();
                    }
                },
                0,      // run first occurrence immediatetly
                5000); // run every 60 seconds
        return START_STICKY;
    }

    @Override
    public boolean stopService(Intent name) {
        // TODO Auto-generated method stub
        t.cancel();
        t.cancel();
        return super.stopService(name);

    }

启动和停止在另一个 Activity 中完成。

public void popupstart() {
        android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(this);
        alertDialog.setTitle("Enable Location Sharing");
        alertDialog.setMessage("Enable location sharing will broadcast your location to clients applications. This is a battery draining process and kindly turn off " +
                "location sharing after use");
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString("shareLocation", "yes");
                editor.commit();
                liveStatus = "1";
                mFab = (FloatingActionButton)findViewById(R.id.fab);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mFab.setImageDrawable(ContextCompat.getDrawable(gnavigationActivity.this, R.drawable.locationon));
                }
                else{
                    mFab.setImageDrawable(getResources().getDrawable(R.drawable.locationon));
                }
                if(!isMyServiceRunning(LocationService.class)) {
                    Toast.makeText(gnavigationActivity.this, "Location Sharing started", Toast.LENGTH_LONG).show();
                    processStartService(LocationService.TAG);
                }
                else{
                    Toast.makeText(gnavigationActivity.this, "Location Sharing already started", Toast.LENGTH_LONG).show();
                }
            }
        });
        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertDialog.show();
    }





 public void popupstop() {
        android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(this);
        alertDialog.setTitle("Stop Location Sharing");
        alertDialog.setMessage("You are about to stop location sharing which now will not broadcast location to client users. Are you sure?");
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                processStopService(LocationService.TAG);
                Toast.makeText(gnavigationActivity.this, "Location Sharing Stoped", Toast.LENGTH_LONG).show();
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString("shareLocation", "no");
                editor.commit();
                liveStatus = "0";
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mFab.setImageDrawable(ContextCompat.getDrawable(gnavigationActivity.this, R.drawable.locationoff));
                }
                else{
                    mFab.setImageDrawable(getResources().getDrawable(R.drawable.locationoff));
                }
            }
        });
        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertDialog.show();
    }

 private void processStartService(final String tag) {
        Intent intent = new Intent(getApplicationContext(), LocationService.class);
        intent.addCategory(tag);
        startService(intent);
    }
    private void processStopService(final String tag) {
        Intent intent = new Intent(getApplicationContext(), LocationService.class);
        intent.addCategory(tag);
        stopService(intent);
    }

最佳答案

调用 stopService(intent);

覆盖方法onDestroy将开始

尝试这样做

 @Override
    public void onDestroy(){
        super.onDestroy();
        t.cancel();
        t.cancel();
    }

关于android - 服务不停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43294209/

相关文章:

authentication - 除非启用 SSL 调试,否则 Web 服务 SSL 握手在生产环境中失败

java - getActivity() 在 Android java 服务中使用的其他方式

c# - 为什么 powershell 无法识别 Windows 服务调用的脚本中的 cmdlet?

java - 尝试打开相机导致我的应用程序崩溃

android - facebook android sdk 3.0 登录另一个 Activity 崩溃

android - 无法从 Assets 文件夹附加 excel 文件

android - Twitter SDK 请求代码

java - 为企业 Java 与 C++ 创建 Web 服务应用程序?

service - UDS SID2E 和 SID22

android - 使用 GridView 的自定义适配器