English 中文(简体)
我如何在新的一线服务?
原标题:How do I start a service in a new thread?

我如何在新的一线服务。 我研究了其他问题,但为我干.。 在我服务时,我需要做什么变化?

问题回答

重新命名:Start(final) 意向书,最后定本启动书(Id)_onStart,并使用新的<条码>执行:

 @Override
 public void onStart(final Intent intent, final int startId) {
     Thread t = new Thread("MyService(" + startId + ")") {
         @Override
         public void run() {
             _onStart(intent, startId);
             stopSelf();
         }
     };
     t.start();
 }

 private void _onStart(final Intent intent, final int startId) {
     //Your Start-Code for the service
 }

<>Ampent5和

public void onStart (Intent, int) was depreced at55/2。 本条应改为:StartCommand(Intent, int)

@Override
public int onStartCommand(final Intent intent, final int startId){
    //All code from  onStart()  in above placed here as normal.
}

private void _onStart(final Intent intent, final int startId) {
     //Your Start-Code for the service
}

我不认为你可以在新的一线上开始服务,但你可以做的是,在你的工作上,你可以做些什么。

This is because like the activity, the service has life cycle methods that run on the main thread.

So your service will run on the main thread but it will do the heavy lifting on a new thread that it creates when ever it needs to.

我希望它能够提供帮助。

引用rel=“nofollow”http://developer.android.com/vis/android/app/service.html

"Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done."

In my project, i have someone like this and it s work:

Thread welcomeThread = new Thread() {
@Override
public void run() {
    try {
        super.run();
        while (isMyServiceRunning() != true) {
            sleep(100);
        }
    } catch (Exception e) {
        System.out.println("EXc=" + e);
    } finally {
        Intent i = new Intent(getApplicationContext(), MainPage.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }
}
};

welcomeThread.start();




相关问题
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 ...

热门标签