我有一个奇怪的问题。我有一个带有 ListView 的 Activity,并在 onCreate 的 Activity 中启动一个服务。
当我现在启动应用程序时,在服务完成工作之前不会显示 Activity 的布局。=( 通常该服务应该在后台完成他的工作。
活动
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class DealAlertMainActivity extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(R.id.list);
//start service if its not running
if(isMyServiceRunning() == false)
startService(new Intent(DealAlertMainActivity.this, DealAlert_Service.class));
}
@Override
public void onStart()
{
super.onStart();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("keyword", "Test Keyword");
map.put("title", "Test Title");
mylist.add(map);
map = new HashMap<String, String>();
map.put("keyword", "Test Keyword 2");
map.put("title", "Test Title 2");
mylist.add(map);
SimpleAdapter feeds_list = new SimpleAdapter(this, mylist, R.layout.listview_item,
new String[] {"keyword", "title"}, new int[] {R.id.Keyword, R.id.Title});
list.setAdapter(feeds_list);
}
...
}
服务
import java.util.ArrayList;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class DealAlert_Service extends Service {
private NotificationManager mNM;
public void onCreate()
{
super.onCreate();
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
public int onStartCommand(Intent intent, int flags, int startId) {
//Do work
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
感谢您的帮助!