2016-03-08 25 views

回答

0

ServiceClass.java(IntentService開始在一個新的線程,我們並不需要它來創建一個新的線程。)

public class IntentServiceDemo extends IntentService { 
      int currentState=0; 

     public IntentServiceDemo() { 
     super("IntentServiceDemoWorker"); // super the name of worker thread, it is necessary. 
    } 
    @Override 
    protected void onHandleIntent(Intent intent) { 
       while (true) { 
        try { 
         Thread.sleep(5000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
         ++currentState: 
        Log.d("vabhi", "currentState : " + currentState); 
       } // End of while 
    } 
} 

Mainifest(內部應用標籤)

<service android:name=".IntentServiceDemo" android:exported="false"></service> 

內MainActivity.java

public void startIntentService(View v) 
{ 
    Intent intent = new Intent(MainActivity.this,IntentServiceDemo.class); 
    startService(intent); 
} 
相關問題