我在我的service
活动中开始onCreate()
,代码如下:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
application = (OldBookApplication) this.getApplicationContext();
util=new SharePreferenceUtil(LoginActivity.this, Constants.SAVE_USER);
Utils.getScreenWidth(this);
Utils.getScreenHeight(this);
Utils.getScreenDensity(this);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());
if (isNetworkAvailable())
{
Intent service = new Intent(LoginActivity.this, GetMsgService.class);
startService(service);
}
else
{
//show("no network");
}
}
@Override
protected void onResume()
{
super.onResume();
checkServer();
autoLogin();
}
和checkServer()
功能如下:
private void checkServer()
{
if(!application.isClientStart())
{
Intent intent=new Intent(LoginActivity.this,SettingActivity.class);
LoginActivity.this.startActivity(intent);
LoginActivity.this.finish();
}
}
我的服务代码如下:
public class GetMsgService extends Service
{
private OldBookApplication application;
private Client client;
private NotificationManager mNotificationManager;
private boolean isStart = false;
private Notification mNotification;
private Context mContext = this;
private SharePreferenceUtil util;
private MessageDB messageDB; //<-----
private BroadcastReceiver backKeyReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Toast.makeText(context, "", Toast.LENGTH_LONG).show();
setMsgNotification();
}
};
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MSG:
......
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());
messageDB = new MessageDB(this);
IntentFilter filter = new IntentFilter();
filter.addAction(Constants.BACKKEY_ACTION);
registerReceiver(backKeyReceiver, filter);
mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
application = (OldBookApplication) this.getApplicationContext();
client = application.getClient();
application.setmNotificationManager(mNotificationManager);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flag, int startId)
{
super.onStartCommand(intent,flag, startId);
util = new SharePreferenceUtil(getApplicationContext(),
Constants.SAVE_USER);
isStart = client.start();
application.setClientStart(isStart);
if (isStart)
{
ClientInputThread in = client.getClientInputThread();
in.setMessageListener(new MessageListener()
{
@Override
public void Message(MessageEntity msg)
{
if (util.getIsStart())
{
if (msg.getType() == MessageType.MESSAGE)
{
......
}
}
else
{
Intent broadCast = new Intent();
broadCast.setAction(Constants.ACTION);
broadCast.putExtra(Constants.MSGKEY, msg);
sendBroadcast(broadCast);
}
}
});
}
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy()
{
super.onDestroy();
if (messageDB != null)
messageDB.close();
unregisterReceiver(backKeyReceiver);
mNotificationManager.cancel(Constants.NOTIFY_ID);
}
}
程序的运行顺序是
LoginActivity的onCreate()
---&gt; LoginActivity的onResume()
---&GT; autoLogin()
功能
---&gt; SettingActivity的onCreate()
---&gt; GetMsgService的onCreate()
---&gt; GetMsgService的onStartCommand()
我想知道为什么在onCreate()
没有调用GetMsgService的startService()
?和SettingActivity一样的问题。
答案 0 :(得分:0)
Start your Service in Application class which extends Application so when your
应用程序启动首先启动您的服务。
答案 1 :(得分:0)
方法startActivity()
和startService()
以异步方式运行。在执行请求的操作之前,对这些方法的调用将返回。从概念上讲,请求的操作将添加到系统事件队列中,并在以后执行。