English 中文(简体)
如何完全停止地面服务?
原标题:How to stop back ground service completely..?

嗨,朋友,我有一个问题要解决...我想彻底摧毁这个服务, 一旦我从活动那里叫Destroy () 的方法。但我的问题是我不能完全摧毁它...在它继续运行的背景中, 我分享我尝试过的样本代码。

    //Activity Class
    public class ServiceToAct extends Activity {
    private static final String TAG = "BroadcastEvent";
    private Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        intent = new Intent(this, BroadcastService.class);
        startService(intent);
        registerReceiver(broadcastReceiver, new       IntentFilter(myService.BROADCAST_ACTION));
    }

    /*@Override
    protected void onResume() {
        super.onResume();

    }*/

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);

    }

    @Override
    protected void onDestroy() {

        stopService(intent);

        Toast.makeText(this, "Destroy Completely", Toast.LENGTH_LONG).show();
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

        }
    };
}

// service class
public class myService extends Service {

    private static final String TAG = "BroadcastEvent";
    public static final String BROADCAST_ACTION = "com.service.activity.myService";
    private final Handler handler = new Handler();
    Intent intent;


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

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "created", Toast.LENGTH_LONG).show();
        intent = new Intent(BROADCAST_ACTION);
    } 

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "start", Toast.LENGTH_LONG).show();
        handler.removeCallbacks(sendToUI);
        handler.postDelayed(sendToUI, 1000); 
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
        //stopService(intent);
        Toast.makeText(this, "Destroy", Toast.LENGTH_LONG).show();
    }

    private Runnable sendToUI = new Runnable() {

        @Override
        public void run() {
            myData();
            handler.postDelayed(this, 10000); 
        }
    };

    private void myData() {

        Log.d(TAG, "keep on entering");
        Toast.makeText(this, "Keep on despling in UI", Toast.LENGTH_LONG).show();

        sendBroadcast(intent);
    }
}

Here Actually i want to update my UI from service, Mine everything is working, but if i destroy the service its keep on calling myData() method, and i am getting the Toast msg if i close the application also. My issue is i don t want that toast msg once the service is desroyed I used stopService(intent) method, which destroy the service, but background method myData() is keep on calling

最佳回答

停止服务完全使用此功能 。

" 强力 " 我的积极性.java

@Override
    public void onDestroy() {
      stopService(intent); 
        super.onDestroy();
        Toast.makeText(this, "Destroy", Toast.LENGTH_LONG).show();
    }

<强力 > 服务.java

@Override
    public void onDestroy()
    {

        stopSelf();
        super.onDestroy();
    }
问题回答

You d better never call onXxx() derectly. Use stopService(Intent i) in your activity and stopSelf() in you service to stop instead.

更新 UI 后使用停止Service () 方法

当活动摧毁时,服务也摧毁





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签