【问题标题】:onstart and onstop of service服务的启动和停止
【发布时间】:2014-07-07 07:56:14
【问题描述】:

我正在编写this 中的服务教程 ..我能够让它完美运行我所做的尝试是在Oncreate 上启动服务,它确实让它在后退按钮上停止但不幸的是@ 987654324@ 不起作用,因为它没有显示吐司,这里是我的代码..是什么导致 onstop 不起作用

public void onBackPressed() {


        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

        builder.setTitle("Confirm Close");

        builder.setMessage("Are you sure you want to exit?");

        builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                stopService(new Intent(this, MyService.class));
                System.exit(0);
                finish();
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alertdialog=builder.create();
        alertdialog.show();
    }

服务代码

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service{

    private static final String TAG = "MyService";

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    //Note: You can start a new thread and use it for long background processing from here.
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }
}

【问题讨论】:

  • 发布 logcat 错误。
  • 只需调用finish() 而不是System.exit(0);finish(); android.os.Process.killProcess(android.os.Process.myPid());
  • @HakHak 是的,它将终止应用程序。除了finish(),你什么都不需要
  • @HakHak :肯定是的。
  • @HakHak 是的,它工作并停止服务,然后停止您应用程序中的所有其他内容。当您删除这些行时,一切正常:它停止服务和应用程序,但以“系统”方式,应该是这样。请不要在您不了解这些方法时使用它们。至少阅读这个帖子:stackoverflow.com/questions/11035328/…

标签: android android-service back


【解决方案1】:

用下面的代码替换你的代码

    public void onBackPressed() {


            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

            builder.setTitle("Confirm Close");

            builder.setMessage("Are you sure you want to exit?");

            builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    stopService(new Intent(this, MyService.class));
                   Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alertdialog=builder.create();
            alertdialog.show();
        }

【讨论】:

  • 删除 System.exit(0);finish(); android.os.Process.killProcess(android.os.Process.myPid());先生成功了,但是为您的努力+ 1..
猜你喜欢
  • 1970-01-01
  • 2014-01-01
  • 2023-04-02
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2018-12-20
  • 1970-01-01
相关资源
最近更新 更多