简体   繁体   English

取消主要活动后重新创建Android服务

[英]Android service recreated when main activity destroyed

I have a problem with android service, When i close the app the main activity closed and the service is recreated - oncreated method call automatic and onstart also called automatic - and all the state is gone. 我在使用android服务时遇到问题,当我关闭该应用程序时,关闭了主要活动,并重新创建了该服务-oncreated方法调用了auto,onstart也称为自动-并且所有状态都消失了。

This is my activity code 这是我的活动代码

   public class ServicesDemo extends Activity implements OnClickListener
{
    private static final String TAG = "ServicesDemo";
    Button buttonStart, buttonStop;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        if (savedInstanceState != null)
        {
            Log.d(TAG, "ServicesDemo:onCreate WITH savedInstanceState)");
        }
        Log.d(TAG, "ServicesDemo:onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        buttonStart = (Button) findViewById(R.id.buttonStart);
        buttonStop = (Button) findViewById(R.id.buttonStop);

        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);
    }

    public void onClick(View src)
    {
        switch (src.getId())
        {
        case R.id.buttonStart: 
            Log.d(TAG, "onClick: starting srvice");
            startService(new Intent(this, MyService.class));
            break;
        case R.id.buttonStop:
            // Log.d(TAG, "onClick: stopping srvice");

             stopService(new Intent(this, MyService.class));
            break;
        }
    }
}

This is the service Code: 这是服务代码:

public class MyService extends Service
{
    private static final String TAG = "ServicesDemo";
    private static MyThread t = new MyThread();
    static int yy = 90;

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

    @Override
    public void onCreate()
    {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
        // MockGPSLocationModel.getInstance().Counter++;

        // Log.d(TAG, "MockGPSLocationModel.getInstance().Counter :: " +
        // MockGPSLocationModel.getInstance().Counter);
        // MockGPSLocationModel.getInstance().Counter++;
    }

    static public class MyThread extends Thread
    {
        MediaPlayer player;
        public Context ctx;

        @Override
        public void run()
        {
            try
            {
                for (int i = 0; i < 100; i++)
                {
                    Log.i(TAG, "lOOP - " + i);
                    Thread.sleep(2000);
                }
                player = MediaPlayer.create(ctx, R.raw.braincandy);
                player.setLooping(false); // Set looping
                player.start();
            }
            catch (Exception e)
            {
                Log.e(TAG, e.toString());

            }
            finally
            {

            }
        }
    }

    @Override
    public void onDestroy()
    {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        // player.stop();
    }

    @Override
    public void onStart(Intent intent, int startid)
    {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");

        t.ctx = this;
        t.start();

        // player.start();
    }
}

AndroidManifest.xml AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".ServicesDemo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true" />
    </application>

    <uses-sdk android:minSdkVersion="3" />

</manifest>

Please help. 请帮忙。 I stuck with that 3 days. 我坚持了那三天。

Thx, Alon Thx,阿隆

The first problem with your code is that you allow starting the thread in MyServcie class multiple times. 代码的第一个问题是,您允许多次在MyServcie类中启动线程。 Each time a user presses the "Start" button, the thread start method is called, which is illegal according to Java API specification : 每次用户按下“开始”按钮,都会调用线程start方法,根据Java API规范 ,该方法是非法的:

It is never legal to start a thread more than once. 一次启动一个线程永远是不合法的。 In particular, a thread may not be restarted once it has completed execution. 特别是,线程一旦完成执行就可能不会重新启动。

You should defend against repeated starting the thread in your Service class for example by using a flag telling whether the thread has been started. 您应防止重复启动服务类中的线程,例如,通过使用一个标志来告知线程是否已启动。

Please also note that Service#onStart method is deprecated since API level 5. Instead, you should use Service#onStartCommand if possible. 另请注意,自API级别5起,不赞成使用Service#onStart方法。相反,应尽可能使用Service#onStartCommand From this method, as Sanket noted, you should return START_NOT_STICKY if you want to execute the code of MyThread#run method only once. 如Sanket所述,从该方法开始,如果只想执行MyThread#run方法的代码,则应返回START_NOT_STICKY Also you may want to explicitly stop the service in the finally block. 另外,您可能想在finally块中明确停止该服务。

看看是否有帮助-使用共享首选项为状态保存一些字符串,并在活动重新打开时取回。

use this method in service class 在服务类中使用此方法

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.

        // do your logic here not in onStart().

        return START_NOT_STICKY;
    }

START_NOT_STICKY and START_STICKY START_NOT_STICKY和START_STICKY

here nice answer for START_NOT_STICKY and START_STICKY 这是START_NOT_STICKY和START_STICKY的不错答案

https://stackoverflow.com/a/9441795/942224 https://stackoverflow.com/a/9441795/942224

and if you want to stop service when app close than use stopService in onDestroy 如果您想在应用关闭时停止服务,则可以在onDestroy使用stopService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM