2012-02-07 95 views
1

我正在創建一個與BroadcastReceiverService相關的應用程序。爲什麼我的服務停止了?

我的意圖是,我的服務從啓動開始。

爲此,我使用廣播接收器並通過它啓動我的服務。

它會正常工作,我的服務類看起來像休閒。

public class Myservice extends Service 
{ 
    public void onCreate() 
    { 
     super.onCreate(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     super.onStart(intent, startId); 

     mScrollback = new Vector<String>(); 
     Thread thr = new Thread(worker); 
     thr.start();   
    } 

    Runnable worker = new Runnable() 
    { 
     public void run() 
     { 
      runLog(); 
      return; 
     } 
    }; 
} 

這是我的服務類,它工作正常。

在我的應用程序開始之前,我通過此應用程序獲取有關服務的信息 。

當我的應用程序運行時,服務沒有運行。

如果任何人有解決方案,請讓我知道。

+0

時退房下面的帖子:http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android – ramdroid 2012-02-07 12:48:52

回答

1

那不是你該怎樣在服務多線程。查看「擴展服務類」步驟:http://developer.android.com/guide/topics/fundamentals/services.html

服務類中的多線程使用處理程序線程完成。下面是從文檔的代碼(從上面的鏈接)

public class HelloService extends Service { 
    private Looper mServiceLooper; 
    private ServiceHandler mServiceHandler; 

    // Handler that receives messages from the thread 
    private final class ServiceHandler extends Handler { 
     public ServiceHandler(Looper looper) { 
      super(looper); 
     } 
     @Override 
     public void handleMessage(Message msg) { 
      // Normally we would do some work here, like download a file. 
      // For our sample, we just sleep for 5 seconds. 
      long endTime = System.currentTimeMillis() + 5*1000; 
      while (System.currentTimeMillis() < endTime) { 
       synchronized (this) { 
        try { 
         wait(endTime - System.currentTimeMillis()); 
        } catch (Exception e) { 
        } 
       } 
      } 
      // Stop the service using the startId, so that we don't stop 
      // the service in the middle of handling another job 
      stopSelf(msg.arg1); 
     } 
    } 

    @Override 
    public void onCreate() { 
    // Start up the thread running the service. Note that we create a 
    // separate thread because the service normally runs in the process's 
    // main thread, which we don't want to block. We also make it 
    // background priority so CPU-intensive work will not disrupt our UI. 
    HandlerThread thread = new HandlerThread("ServiceStartArguments", 
      Process.THREAD_PRIORITY_BACKGROUND); 
    thread.start(); 

    // Get the HandlerThread's Looper and use it for our Handler 
    mServiceLooper = thread.getLooper(); 
    mServiceHandler = new ServiceHandler(mServiceLooper); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 

     // For each start request, send a message to start a job and deliver the 
     // start ID so we know which request we're stopping when we finish the job 
     Message msg = mServiceHandler.obtainMessage(); 
     msg.arg1 = startId; 
     mServiceHandler.sendMessage(msg); 

     // If we get killed, after returning from here, restart 
     return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // We don't provide binding, so return null 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
    } 
} 

而且你必須確保你的廣播接收裝置,清單中列出,並接收BOOT_COMPLETED意圖:

<receiver android:name="MyBroadCast"> 
<intent-filter> 
<action android:name="android.intent.action.BOOT_COMPLETED" /> 
</intent-filter> 
</receiver> 
0

在設備重新啓動後,您需要授予啓動服務的權限。 你可以給權限如下:

<service android:enabled="true" android:name=".Myservice" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 

<intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 

     </intent-filter> 
+1

你可能想包括實際後面的內容...... – 2012-02-07 12:13:13

相關問題