修改某些机型 后台长时间运行后 推送服务被kill的问题

This commit is contained in:
xiajun 2014-11-19 09:49:39 +08:00
parent e886bd9019
commit ce15aeba68
12 changed files with 131 additions and 16 deletions

View File

@ -13,7 +13,8 @@ import com.farsunset.cim.nio.mutual.SentBody;
*/ */
public class CIMPushManager { public class CIMPushManager {
static String ACTION_CONNECTION_KEEPALIVE ="ACTION_CONNECTION_KEEPALIVE";
static String ACTION_CONNECTION ="ACTION_CONNECTION"; static String ACTION_CONNECTION ="ACTION_CONNECTION";
static String ACTION_CONNECTION_STATUS ="ACTION_CONNECTION_STATUS"; static String ACTION_CONNECTION_STATUS ="ACTION_CONNECTION_STATUS";

View File

@ -1,10 +1,13 @@
package com.farsunset.cim.client.android; package com.farsunset.cim.client.android;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import com.farsunset.cim.nio.mutual.SentBody; import com.farsunset.cim.nio.mutual.SentBody;
@ -18,25 +21,33 @@ import com.farsunset.cim.nio.mutual.SentBody;
CIMConnectorManager manager; CIMConnectorManager manager;
AlarmManager localAlarmManager;
private IBinder binder=new CIMPushService.LocalBinder(); private IBinder binder=new CIMPushService.LocalBinder();
PendingIntent localPendingIntent;
@Override @Override
public void onCreate() public void onCreate()
{ {
manager = CIMConnectorManager.getManager(this.getApplicationContext()); manager = CIMConnectorManager.getManager(this.getApplicationContext());
localPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, KeepAliveReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
localAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
localAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 300000L + System.currentTimeMillis(),300000L, localPendingIntent);
} }
@Override @Override
public int onStartCommand(Intent intent,int flags, int startId) { public int onStartCommand(Intent intent,int flags, int startId) {
String action;
if(intent==null) if(intent==null)
{ {
return super.onStartCommand(intent, flags, startId); intent = new Intent(CIMPushManager.ACTION_CONNECTION);
} String host = CIMDataConfig.getString(this, CIMDataConfig.KEY_CIM_SERVIER_HOST);
int port =CIMDataConfig.getInt(this, CIMDataConfig.KEY_CIM_SERVIER_PORT);
intent.putExtra(CIMDataConfig.KEY_CIM_SERVIER_HOST, host);
intent.putExtra(CIMDataConfig.KEY_CIM_SERVIER_PORT, port);
}
String action = intent.getStringExtra(CIMPushManager.SERVICE_ACTION); action = intent.getStringExtra(CIMPushManager.SERVICE_ACTION);
if(CIMPushManager.ACTION_CONNECTION.equals(action)) if(CIMPushManager.ACTION_CONNECTION.equals(action))
{ {
@ -57,8 +68,11 @@ import com.farsunset.cim.nio.mutual.SentBody;
if(CIMPushManager.ACTION_DESTORY.equals(action)) if(CIMPushManager.ACTION_DESTORY.equals(action))
{ {
localAlarmManager.cancel(localPendingIntent);
localPendingIntent.cancel();
manager.destroy(); manager.destroy();
this.stopSelf(); this.stopSelf();
android.os.Process.killProcess(android.os.Process.myPid());
} }
if(CIMPushManager.ACTION_CONNECTION_STATUS.equals(action)) if(CIMPushManager.ACTION_CONNECTION_STATUS.equals(action))
@ -66,6 +80,22 @@ import com.farsunset.cim.nio.mutual.SentBody;
manager.deliverIsConnected(); manager.deliverIsConnected();
} }
if(CIMPushManager.ACTION_CONNECTION_KEEPALIVE.equals(action))
{
if(!manager.isConnected())
{
Log.d(CIMPushService.class.getSimpleName(), "isConnected() = false ");
String host = intent.getStringExtra(CIMDataConfig.KEY_CIM_SERVIER_HOST);
int port = intent.getIntExtra(CIMDataConfig.KEY_CIM_SERVIER_PORT, 28888);
manager.connect(host,port);
}else
{
Log.d(CIMPushService.class.getSimpleName(), "isConnected() = true ");
}
}
return Service.START_REDELIVER_INTENT; return Service.START_REDELIVER_INTENT;
} }

View File

@ -0,0 +1,24 @@
package com.farsunset.cim.client.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* @author 3979434
*
*/
public class KeepAliveReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent it) {
Log.d(KeepAliveReceiver.class.getSimpleName(), "onReceive()");
Intent intent = new Intent(context, CIMPushService.class);
intent.putExtra(CIMPushManager.SERVICE_ACTION, CIMPushManager.ACTION_CONNECTION_KEEPALIVE);
context.startService(intent);
}
}

View File

@ -51,8 +51,10 @@
android:launchMode="singleTop" android:launchMode="singleTop"
android:screenOrientation="portrait" /> android:screenOrientation="portrait" />
<!--推送服务-->
<service android:name="com.farsunset.cim.client.android.CIMPushService" android:process=":cimpush" /> <service android:name="com.farsunset.cim.client.android.CIMPushService" android:process=":cimpush" />
<!--保持长连接广播-->
<receiver android:name="com.farsunset.cim.client.android.KeepAliveReceiver" android:process=":cimpush"/>
<!--消息接受广播注册--> <!--消息接受广播注册-->
<receiver android:name="com.farsunset.ichat.example.receiver.CustomCIMMessageReceiver" android:exported="false"> <receiver android:name="com.farsunset.ichat.example.receiver.CustomCIMMessageReceiver" android:exported="false">
<intent-filter > <intent-filter >

Binary file not shown.

View File

@ -13,7 +13,8 @@ import com.farsunset.cim.nio.mutual.SentBody;
*/ */
public class CIMPushManager { public class CIMPushManager {
static String ACTION_CONNECTION_KEEPALIVE ="ACTION_CONNECTION_KEEPALIVE";
static String ACTION_CONNECTION ="ACTION_CONNECTION"; static String ACTION_CONNECTION ="ACTION_CONNECTION";
static String ACTION_CONNECTION_STATUS ="ACTION_CONNECTION_STATUS"; static String ACTION_CONNECTION_STATUS ="ACTION_CONNECTION_STATUS";

View File

@ -1,10 +1,13 @@
package com.farsunset.cim.client.android; package com.farsunset.cim.client.android;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import com.farsunset.cim.nio.mutual.SentBody; import com.farsunset.cim.nio.mutual.SentBody;
@ -18,25 +21,33 @@ import com.farsunset.cim.nio.mutual.SentBody;
CIMConnectorManager manager; CIMConnectorManager manager;
AlarmManager localAlarmManager;
private IBinder binder=new CIMPushService.LocalBinder(); private IBinder binder=new CIMPushService.LocalBinder();
PendingIntent localPendingIntent;
@Override @Override
public void onCreate() public void onCreate()
{ {
manager = CIMConnectorManager.getManager(this.getApplicationContext()); manager = CIMConnectorManager.getManager(this.getApplicationContext());
localPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, KeepAliveReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
localAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
localAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 300000L + System.currentTimeMillis(),300000L, localPendingIntent);
} }
@Override @Override
public int onStartCommand(Intent intent,int flags, int startId) { public int onStartCommand(Intent intent,int flags, int startId) {
String action;
if(intent==null) if(intent==null)
{ {
return super.onStartCommand(intent, flags, startId); intent = new Intent(CIMPushManager.ACTION_CONNECTION);
} String host = CIMDataConfig.getString(this, CIMDataConfig.KEY_CIM_SERVIER_HOST);
int port =CIMDataConfig.getInt(this, CIMDataConfig.KEY_CIM_SERVIER_PORT);
intent.putExtra(CIMDataConfig.KEY_CIM_SERVIER_HOST, host);
intent.putExtra(CIMDataConfig.KEY_CIM_SERVIER_PORT, port);
}
String action = intent.getStringExtra(CIMPushManager.SERVICE_ACTION); action = intent.getStringExtra(CIMPushManager.SERVICE_ACTION);
if(CIMPushManager.ACTION_CONNECTION.equals(action)) if(CIMPushManager.ACTION_CONNECTION.equals(action))
{ {
@ -57,8 +68,11 @@ import com.farsunset.cim.nio.mutual.SentBody;
if(CIMPushManager.ACTION_DESTORY.equals(action)) if(CIMPushManager.ACTION_DESTORY.equals(action))
{ {
localAlarmManager.cancel(localPendingIntent);
localPendingIntent.cancel();
manager.destroy(); manager.destroy();
this.stopSelf(); this.stopSelf();
android.os.Process.killProcess(android.os.Process.myPid());
} }
if(CIMPushManager.ACTION_CONNECTION_STATUS.equals(action)) if(CIMPushManager.ACTION_CONNECTION_STATUS.equals(action))
@ -66,6 +80,22 @@ import com.farsunset.cim.nio.mutual.SentBody;
manager.deliverIsConnected(); manager.deliverIsConnected();
} }
if(CIMPushManager.ACTION_CONNECTION_KEEPALIVE.equals(action))
{
if(!manager.isConnected())
{
Log.d(CIMPushService.class.getSimpleName(), "isConnected() = false ");
String host = intent.getStringExtra(CIMDataConfig.KEY_CIM_SERVIER_HOST);
int port = intent.getIntExtra(CIMDataConfig.KEY_CIM_SERVIER_PORT, 28888);
manager.connect(host,port);
}else
{
Log.d(CIMPushService.class.getSimpleName(), "isConnected() = true ");
}
}
return Service.START_REDELIVER_INTENT; return Service.START_REDELIVER_INTENT;
} }

View File

@ -0,0 +1,24 @@
package com.farsunset.cim.client.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* @author 3979434
*
*/
public class KeepAliveReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent it) {
Log.d(KeepAliveReceiver.class.getSimpleName(), "onReceive()");
Intent intent = new Intent(context, CIMPushService.class);
intent.putExtra(CIMPushManager.SERVICE_ACTION, CIMPushManager.ACTION_CONNECTION_KEEPALIVE);
context.startService(intent);
}
}

View File

@ -51,7 +51,10 @@
android:launchMode="singleTop" android:launchMode="singleTop"
android:screenOrientation="portrait" /> android:screenOrientation="portrait" />
<service android:name="com.farsunset.cim.client.android.CIMPushService" /> <!--推送服务-->
<service android:name="com.farsunset.cim.client.android.CIMPushService" android:process=":cimpush" />
<!--保持长连接广播-->
<receiver android:name="com.farsunset.cim.client.android.KeepAliveReceiver" android:process=":cimpush"/>
<!--消息接受广播注册--> <!--消息接受广播注册-->
<receiver android:name="com.farsunset.ichat.example.receiver.CustomCIMMessageReceiver" android:exported="false"> <receiver android:name="com.farsunset.ichat.example.receiver.CustomCIMMessageReceiver" android:exported="false">

Binary file not shown.