запуск / остановка службы несколько раз не работает - PullRequest
1 голос
/ 30 августа 2010

Я пишу простое приложение службы, ниже приведен код Activity и Service .., когда я звоню startService(), и stopService() работает нормально один раз, в моем случае он должен дать уведомление ... со следующего раза, если снова позвонить startService(), и stopService() не даст желаемых результатов ...

---------- это мой класс занятий -------------

    package com.mypack.serviceex;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;

    public class serviceex extends Activity implements Button.OnClickListener{
        /** Called when the activity is first created. */
        Button bt1,bt2;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            bt1 = (Button) findViewById(R.id.Button01);
            bt2 = (Button) findViewById(R.id.Button02);

            bt1.setOnClickListener(this);
            bt2.setOnClickListener(this);


        }
        @Override
    public void onClick(View v) {
        if( v == bt1)
        {
            Intent i = new Intent(serviceex.this,myservice.class);
            Log.i("err","onClick(View v....");
            startService(i);
        }
        else if(v == bt2)
        {
            Intent i = new Intent(serviceex.this,myservice.class);
            Log.i("err","else if(v == bt2)........");
            stopService(i);
        }

    }
}

--------- это мой сервис -------------

package com.mypack.serviceex;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;


public class myservice extends Service
{

    private NotificationManager nmgr;
    public void onCreate()
    {
        super.onCreate();
        nmgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Log.i("err","onCreate..........");
        Thread th = new Thread(null,new incls(),"service...");

    }
    public void onStart(Intent intent,int sid)
    {
        super.onStart(intent, sid);
        Log.i("err","onStart........");
    }
    public void onDestroy()
    {
        super.onDestroy();
        Log.i("err","onDestroy..........");
        displayMessage("Stopping Service");

    }
    public void displayMessage(String str)
    {
        Log.i("err.","displayMessage.....");
        Notification nf = new Notification(R.drawable.icon,str,System.currentTimeMillis());
        PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,myservice.class), 0);
        nf.setLatestEventInfo(this, "Service...", str, pi);
        nmgr.notify(R.string.uid, nf);


    }

    private class incls implements Runnable
    {
        public void run()
        {
            Log.i("err","public void run()..........");
            System.out.println("In Runnn");
        }
    }
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

1 Ответ

0 голосов
/ 31 августа 2010

Ни один из ваших методов @Override не использует методы класса. Вы должны пометить их @Override. Также в 2.1 вы должны использовать onStartCommand() вместо onStart(). И также обратите внимание, что вызов startService() несколько раз вызовет onCreate() только один раз

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...