是否可以使用Web服务运行后台服务,仍然可以从最近的应用历史记录中打开并清除活动

时间:2015-08-19 06:01:59

标签: java android performance service

我尝试过如下解决我的问题,但如果从应用历史记录服务中移除应用程序未运行,我仍然会发现问题。

在我的活动中:

   // start background service by splash screen
    startService(new Intent(this, UpdateService.class));

在我的清单中:

    <application
    android:allowBackup="true"
    android:icon="@drawable/demo"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service android:name=".UpdateService"/>

    </application>

在我的服务中:

public class UpdateService extends IntentService {
public UpdateService() {
super("UpdateService");
}

Handler mHandler = new Handler();
int Updatevalue = 0;

Context gContext= this;

@Override
 public IBinder onBind(Intent arg0) {
 return null;
 }
@Override
 public int onStartCommand(Intent intent, int flags, int startId) {
 // Let it continue running until it is stopped.

 Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

 new Thread(new Runnable() {
 @Override
 public void run() {
// TODO Auto-generated method stub
while (true) {
try {
// 10 sec
Thread.sleep(5000);// 1000*60= 1 min ;;; 1000 = 1 sec ;;; 1000*60*2= 2 min
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(gContext, "Service running", Toast.LENGTH_LONG).show();   
 // web service implement for 2 min updating...
}
});
} catch (Exception e) {
}
}
}
}).start();        
return START_STICKY;
}
@Override
 public void onDestroy() {
 Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
@Override
protected void onHandleIntent(Intent intent) {

 // Let it continue running until it is stopped.
 Toast.makeText(this, "Service Started in handle Intent", 
    Toast.LENGTH_LONG).show();
 new Thread(new Runnable() {
 @Override
   public void run() {
 // TODO Auto-generated method stub
 while (true) {
 try {
// 10 sec
Thread.sleep(5000);// 1000*60= 1 min ;;; 1000 = 1 sec ;;; 1000*60*2= 2 min
 mHandler.post(new Runnable() {
 @Override
 public void run() {
Toast.makeText(gContext, "Service running in handle Intent ",   
Toast.LENGTH_LONG).show();   
 // web service implement for 2 min updating...
}
});
} catch (Exception e) {
 }
 }
}
 }).start();        
}
}

如果我们不打开应用程序,我想要一个什么样的功能然后我们收到什么的通知。所以我需要webservice这个功能。这将在后台运行,以便以JSON格式从webservice接收任何数据。

1 个答案:

答案 0 :(得分:0)

我通过清单中的更改获得了解决方案:

<service android:name=".UpdateService" >
   </service>

并在“onStartCommand”方法中添加super关键字,如:

super.onStartCommand(intent, startId, startId);

并更改“onStartCommand”方法的返回,如:

return START_REDELIVER_INTENT;

感谢