2014-03-01 36 views
0

我想在Android Studio中創建一個Android應用程序。 該應用程序主要是一個後臺服務,每15秒左右發送一些信息到服務器。 我已經創建了一個空的活動,啓動接收器和一個服務類,但它不起作用。 有人可以幫助我,並解釋爲什麼它不起作用。後臺服務不起作用

相關文件:

MainActivity.java

package nl.robinvandervliet.robinsapp.app; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 

public class MainActivity extends ActionBarActivity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     finish(); 
    } 
} 

BootReceiver.java

package nl.robinvandervliet.robinsapp.app; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class BootReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     context.startService(new Intent(context, RobinsService.class)); 
    } 
} 

RobinsService.java

package nl.robinvandervliet.robinsapp.app; 

import android.app.Notification; 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class RobinsService extends Service 
{ 
    private Runnable runnable = new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      //Opening connection to the server. 

      while (true) 
      { 
       //Sending some data to the server. 
       //Sleeping for around 15 seconds. 
      } 

      //Dead code, should never reach this place. 
     } 
    }; 

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

    @Override 
    public void onCreate() 
    { 
     new Thread(runnable).start(); 

     Notification notification = new Notification.Builder(getApplicationContext()).setContentTitle("MyService is running!").setContentTitle("(more information later)").build(); 
     startForeground(1, notification); 
     Toast.makeText(getApplicationContext(), "Service created!", Toast.LENGTH_LONG).show(); 
    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="nl.robinvandervliet.robinsapp.app" > 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="nl.robinvandervliet.robinsapp.app.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name="nl.robinvandervliet.robinsapp.app.BootReceiver" android:enabled="true" android:exported="false"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

     <service android:name="nl.robinvandervliet.robinsapp.app.RobinsService" android:exported="false" /> 
    </application> 

    <uses-permission android:name="android.permission.BATTERY_STATS" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

</manifest> 

謝謝,羅賓。

+0

該應用程序本身沒有GUI,因此我稱之爲「finish();」手動啓動應用後立即關閉它。 –

+0

嘗試在您的服務中使用onStartCommand方法,並通過asynctask執行所有線程操作。 – Ranjit

+0

如果您只啓動一個線程,爲什麼在您的服務中使用Runnable?你有多線程計劃嗎?該服務應該由OS適當地處理。 – indivisible

回答

0

由於您尚未實現某些部件,我假設您只是希望它在引導設備時運行。也許看看here。在清單中添加額外的操作可能會解決您的問題。