从android中的sherlock片段启动/绑定服务时出错

时间:2013-12-01 10:33:47

标签: android actionbarsherlock android-service

我正试图从sherlock片段类启动并盲目服务,并且由于某种原因,应用程序在启动/绑定服务时只是崩溃!相同的代码没有碎片为我工作!我还尝试在清单文件中为服务添加完整的包名,但仍然无效!

public class Client extends SherlockFragment implements Interface,
        OnClickListener {

    Button play;
    TextView title;

    // Bass Service
    private Server background = null;

    // Bass Service Connection
    private ServiceConnection bgLink = new ServiceConnection() {

        public void onServiceConnected(ComponentName name, IBinder service) {
            background = ((Server.ServerBinder) service).getService();
            onServerConnected();
        }

        public void onServiceDisconnected(ComponentName name) {
            background = null;
        }

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_player, container,
                false);

        play = (Button) rootView.findViewById(R.id.btPlay);
        play.setOnClickListener(this);
        title = (TextView) rootView.findViewById(R.id.radio_status);

        // Start Service
        getActivity().startService(new Intent(getActivity(),Server.class));

        // Bind Service
        getActivity().bindService(new Intent(getActivity(), Server.class),
                bgLink, 1);

        return rootView;
    }

    @Override
    public void onDestroy() {
        background.unsetActivity();

        // Unbind Service
        getActivity().unbindService(bgLink);

        super.onDestroy();
    }

    // onServerConnected: Put some activity stuff here
    public void onServerConnected() {

        // Register Activity
        background.setActivity(this);
    }

    public void switchPlaying() {
        if (background.getStatus() != 2) {
            if (background.getStatus() == 1) { // Playing
                background.Stop();
                statusUpdate(getString(R.string.welcome));
            } else { // Idle
                statusUpdate(getString(R.string.connecting));
                background.Play();
            }
        }
    }


    public void msg(String text, int duration) {
        Toast.makeText(getActivity(), text, duration).show();
    }

    public void statusUpdate(String text) {
        title.setText(text);
    }

    @Override
    public void onNewTrack(final String meta) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                statusUpdate(meta);
            }
        });
    }

    @Override
    public void onStoppedPlayback() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                statusUpdate(getString(R.string.welcome));
            };
        });
    }

    @Override
    public void onError(final String msg) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                msg(msg, 1);
            }
        });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btPlay: {
            switchPlaying();
        }
        }
    }
}

Log Cat输出:

12-01 05:28:16.155: E/AndroidRuntime(2747): FATAL EXCEPTION: main
12-01 05:28:16.155: E/AndroidRuntime(2747): Process: com.inspiron.app, PID: 2747
12-01 05:28:16.155: E/AndroidRuntime(2747): java.lang.UnsatisfiedLinkError: Couldn't load bass from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.inspiron.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.inspiron.app-1, /system/lib]]]: findLibrary returned null
12-01 05:28:16.155: E/AndroidRuntime(2747):     at java.lang.Runtime.loadLibrary(Runtime.java:358)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at java.lang.System.loadLibrary(System.java:526)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at com.un4seen.bass.BASS.<clinit>(BASS.java:675)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at com.inspiron.app.Server.onCreate(Server.java:58)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.app.ActivityThread.access$1700(ActivityThread.java:135)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1479)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.os.Looper.loop(Looper.java:137)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invoke(Method.java:515)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-01 05:28:16.155: E/AndroidRuntime(2747):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

实际上我不知道你的代码有什么问题,但我总是使用这个概念(与Messenger的进程间通信)

 private Messenger messenger;
private ServiceConnection conn = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder binder) {
        messenger = new Messenger(binder);
    }

    public void onServiceDisconnected(ComponentName className) {
        messenger = null;
    }
};

和onBind服务方法应该是这样的

@Override
public IBinder onBind(Intent arg0) {
    Bundle extras = arg0.getExtras();
    if (extras != null) {
        outMessenger = (Messenger) extras.get("MESSENGER");
    }
    return inMessenger.getBinder();
}

你应该使用这种方法与Handler

// In the activity
private Handler myHandler = new Handler() {

    public void handleMessage(android.os.Message msg) {
    }
}

// In the service
private class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
     }
 }

希望有用