无法绑定到TabHost中的远程服务

时间:2012-02-10 18:39:21

标签: android service bind

我正在写一个简单的秒表和倒计时应用程序。应用程序的不同部分由选项卡表示。因此我的主要活动看起来像这样:

@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);

所以我猜服务没有正确绑定。

我的应用需要什么? 我有秒表活动和倒计时活动,可以单独启动计时器。我想要时间运行,即使应用程序已关闭,所以我需要一个服务来运行两个计时器。 但是我无法将这两个活动绑定到服务上! 我做错了什么?

提前致谢 FLO

0 个答案:

没有答案