无法绑定到 TabHost 中的远程服务
我正在编写一个简单的秒表和倒计时应用程序。应用程序的不同部分由选项卡表示。因此,我的主要活动如下所示:
@SuppressWarnings("deprecation")
public class ITimeActivity extends TabActivity {
StopwatchInterface service;
Intent mServiceIntent;
RemoteServiceConnection conn;
class RemoteServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder binder) {
service = StopwatchInterface.Stub.asInterface((IBinder) binder);
}
public void onServiceDisconnected(ComponentName name) {
service = null;
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Service starten
Intent i = new Intent();
i.setClassName("com.BlubBlub.iTime", "com.BlubBlub.iTime.RemoteService");
startService(new Intent(this, StopwatchService.class));
final TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab1.class);
spec = tabHost.newTabSpec("tab1").setIndicator(getString(R.string.wecker)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("tab2").setIndicator(getString(R.string.stoppuhr)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab3.class);
spec = tabHost.newTabSpec("tab3").setIndicator(getString(R.string.countdown)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab4.class);
spec = tabHost.newTabSpec("tab4").setIndicator(getString(R.string.weltzeit)).setContent(intent);
tabHost.addTab(spec);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
if(prefs.getString("tabstart", "0") == "4") {
SharedPreferences myPrefs = getSharedPreferences("iTime.settings", MODE_WORLD_READABLE);
tabHost.setCurrentTab(myPrefs.getInt("start", 0));
}
else {
tabHost.setCurrentTab(Integer.parseInt(prefs.getString("tabstart", "0")));
}
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String arg0) {
SharedPreferences myPrefs = getSharedPreferences("iTime.settings", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt("start", getTabHost().getCurrentTab());
prefsEditor.commit();
}
});
}
public void onDestroy() {
super.onDestroy();
unbindService(conn);
try {
if(service.getServiceState() == false) {
Intent i = new Intent();
i.setClassName("com.BlubBlub.iTime", "com.BlubBlub.iTime.RemoteService");
stopService(i);
}
} catch (RemoteException re) {
}
}
}
在这个类中,我创建了服务,效果很好。 现在让我们看看服务本身。 首先是aidl 文件:
interface StopwatchInterface {
boolean getServiceState();
long getElapsed();
void setStartStopwatch(long i);
void startStop();
void stopStop();
void setRefreshStopwatch(int j);
}
现在是服务的相关java 部分:
public class StopwatchService extends Service {
public IBinder onBind(Intent intent) {
return myStopwatchServiceStub;
}
public boolean onUnbind(Intent intent) {
return false;
}
public int onStartCommand(Intent intent, int param1, int param2) {
return 0;
}
private StopwatchInterface.Stub myStopwatchServiceStub = new StopwatchInterface.Stub() {
public boolean getServiceState() throws RemoteException {
return running;
}
public long getElapsed() {
return elapsedTime;
}
public void setStartStopwatch(long i) {
startStopwatch = i;
}
public void startStop() {
mHandler1.removeCallbacks(stopwatch);
mHandler1.postDelayed(stopwatch, 0);
stopwatchrunning = true;
running = true;
}
public void stopStop() {
mHandler1.removeCallbacks(stopwatch);
stopwatchrunning = false;
if(countdownrunning == false) {
running = false;
}
}
public void setRefreshStopwatch(int i) {
refreshStopwatch = i;
}
};
}
最后是秒表活动的相关部分:
public class Tab2 extends Activity {
StopwatchInterface service;
Intent mServiceIntent;
RemoteServiceConnection conn;
class RemoteServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder binder) {
service = StopwatchInterface.Stub.asInterface((IBinder) binder);
}
public void onServiceDisconnected(ComponentName name) {
service = null;
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stoppuhr);
}
public void onResume() {
super.onResume();
mServiceIntent = new Intent();
mServiceIntent.setClassName("com.BlubBlub.iTime", "com.BlubBlub.iTime.RemoteService");
conn = new RemoteServiceConnection();
bindService(mServiceIntent, conn, Context.BIND_AUTO_CREATE);
try {
service.setRefreshStopwatch(1);
} catch (RemoteException re) {
}
REFRESH_RATE = 1;
}
}
当我启动应用程序时,焦点会自动设置到Tab2 类。但在 onResume Methode 中,我在该行收到 NullPointerException
service.setRefreshStopwatch(1);
所以我猜该服务未正确绑定。
我的应用程序需要什么? 我有一个秒表活动和一个倒计时活动,其中可以单独启动计时器。即使应用程序关闭,我也希望时间能够运行,因此我需要一个服务来运行两个计时器。 但我无法将这两个活动绑定到服务! 我做错了什么?
提前致谢 弗洛
I'm writing a simple stopwatch and countdown app. The different parts of the app are represented by tabs. Therefore my main Activity looks like this:
@SuppressWarnings("deprecation")
public class ITimeActivity extends TabActivity {
StopwatchInterface service;
Intent mServiceIntent;
RemoteServiceConnection conn;
class RemoteServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder binder) {
service = StopwatchInterface.Stub.asInterface((IBinder) binder);
}
public void onServiceDisconnected(ComponentName name) {
service = null;
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Service starten
Intent i = new Intent();
i.setClassName("com.BlubBlub.iTime", "com.BlubBlub.iTime.RemoteService");
startService(new Intent(this, StopwatchService.class));
final TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab1.class);
spec = tabHost.newTabSpec("tab1").setIndicator(getString(R.string.wecker)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("tab2").setIndicator(getString(R.string.stoppuhr)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab3.class);
spec = tabHost.newTabSpec("tab3").setIndicator(getString(R.string.countdown)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab4.class);
spec = tabHost.newTabSpec("tab4").setIndicator(getString(R.string.weltzeit)).setContent(intent);
tabHost.addTab(spec);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
if(prefs.getString("tabstart", "0") == "4") {
SharedPreferences myPrefs = getSharedPreferences("iTime.settings", MODE_WORLD_READABLE);
tabHost.setCurrentTab(myPrefs.getInt("start", 0));
}
else {
tabHost.setCurrentTab(Integer.parseInt(prefs.getString("tabstart", "0")));
}
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String arg0) {
SharedPreferences myPrefs = getSharedPreferences("iTime.settings", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt("start", getTabHost().getCurrentTab());
prefsEditor.commit();
}
});
}
public void onDestroy() {
super.onDestroy();
unbindService(conn);
try {
if(service.getServiceState() == false) {
Intent i = new Intent();
i.setClassName("com.BlubBlub.iTime", "com.BlubBlub.iTime.RemoteService");
stopService(i);
}
} catch (RemoteException re) {
}
}
}
In this class I create the service, which works good.
Now let's have a look at the service itself.
First the aidl File:
interface StopwatchInterface {
boolean getServiceState();
long getElapsed();
void setStartStopwatch(long i);
void startStop();
void stopStop();
void setRefreshStopwatch(int j);
}
and now the relevant java part of the service:
public class StopwatchService extends Service {
public IBinder onBind(Intent intent) {
return myStopwatchServiceStub;
}
public boolean onUnbind(Intent intent) {
return false;
}
public int onStartCommand(Intent intent, int param1, int param2) {
return 0;
}
private StopwatchInterface.Stub myStopwatchServiceStub = new StopwatchInterface.Stub() {
public boolean getServiceState() throws RemoteException {
return running;
}
public long getElapsed() {
return elapsedTime;
}
public void setStartStopwatch(long i) {
startStopwatch = i;
}
public void startStop() {
mHandler1.removeCallbacks(stopwatch);
mHandler1.postDelayed(stopwatch, 0);
stopwatchrunning = true;
running = true;
}
public void stopStop() {
mHandler1.removeCallbacks(stopwatch);
stopwatchrunning = false;
if(countdownrunning == false) {
running = false;
}
}
public void setRefreshStopwatch(int i) {
refreshStopwatch = i;
}
};
}
And at last the relevant part of the stopwatch activity:
public class Tab2 extends Activity {
StopwatchInterface service;
Intent mServiceIntent;
RemoteServiceConnection conn;
class RemoteServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder binder) {
service = StopwatchInterface.Stub.asInterface((IBinder) binder);
}
public void onServiceDisconnected(ComponentName name) {
service = null;
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stoppuhr);
}
public void onResume() {
super.onResume();
mServiceIntent = new Intent();
mServiceIntent.setClassName("com.BlubBlub.iTime", "com.BlubBlub.iTime.RemoteService");
conn = new RemoteServiceConnection();
bindService(mServiceIntent, conn, Context.BIND_AUTO_CREATE);
try {
service.setRefreshStopwatch(1);
} catch (RemoteException re) {
}
REFRESH_RATE = 1;
}
}
When I start my app the focus is set automatically to the Tab2 class. But in the onResume Methode I get a NullPointerException at the line
service.setRefreshStopwatch(1);
So I guess the service is not correctly bound.
What do I need for my app?
I have a stopwatch activity and a countdown activity where a timer can be started seperatly. I want the time to run, even if the app is closed, so I need a service to run the two timers.
But I can't get the two activities to bind to the service!
What am I doing wrong?
Thanks in advance
Flo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论