我对Android编程和Java相当陌生,但作为一个系统级C编码器,我有丰富的经验。我想要创建一个API或库,供我计划开发的一套安卓应用程序使用。经过大量研究,我认为绑定服务是安卓的方式(我通常会创建一个.so或.dll)。如果这个假设是不正确的,那么我的整个问题很可能是毫无意义的。
无论如何,我在一个项目中创建了我的服务框架,在另一个项目中创建了一个活动来测试服务。我通过调用Toast来删除服务的handleMessage()方法,以查看它是否正常工作,然后在我的活动中添加了两个按钮:一个用于绑定服务,另一个用于解除绑定。在由于看不到我的服务而无法运行我的活动大约一个小时之后,我想出了如何让Eclipse从活动中看到服务的源代码,并认为我已经做得很好了。然后我主持了这个活动。
一切看起来都很好。两个按钮,一个去绑定,一个去解开。然而,当我按下绑定按钮时,它会强制关闭应用程序。我使用调试器查看发生了什么,在我的活动中强制关闭bindService()调用。当然,bindService()调用是从另一个示例中复制的,在该示例中,在同一行上创建了一个新意图(),因此我将这两行代码分开,当我试图创建该意图时,它实际上正在消亡。
下面是我的活动代码:
package net.william.android.myAPI.tester;
import net.william.android.myAPI.MyApiService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;
public class MyApiTester extends Activity
{
boolean mIsBound = false;
private ServiceConnection mConnection = new ServiceConnection()
{
public void onServiceConnected(ComponentName className, IBinder service)
{
Toast.makeText(FaceClientTester.this, "Remote Service Connected", Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className)
{
Toast.makeText(FaceClientTester.this, "Remote Service Disconnected", Toast.LENGTH_SHORT).show();
}
};
public void doBindService(View view)
{
if (!mIsBound)
{
Toast.makeText(this, "Binding Service", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MyApiTester.this, MyApiService.class);
bindService(myIntent, mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
}
public void doUnbindService(View view)
{
if (mIsBound)
{
unbindService(mConnection);
mIsBound = false;
Toast.makeText(this, "Service Unbound", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
return;
}
}这是我的服务代码:
package net.william.android.myAPI
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.Toast;
public class MyApiService extends Service
{
static final int API_FN_1 = 101;
static final int API_FN_2 = 102;
class MyApiHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case API_FN_1:
ApiFn1();
break;
case API_FN_2:
ApiFn2();
break;
default:
super.handleMessage(msg);
break;
}
}
}
final Messenger myMessenger = new Messenger(new MyApiHandler());
@Override
public void onCreate()
{
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy()
{
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent)
{
Toast.makeText(this, "Service Bound", Toast.LENGTH_SHORT).show();
return myMessenger.getBinder();
}
private void ApiFn1()
{
Toast.makeText(this, "API Function 1", Toast.LENGTH_SHORT).show();
return;
}
private void ApiFn2()
{
Toast.makeText(this, "API Function 2", Toast.LENGTH_SHORT).show();
return;
}
}我更改了一些东西的名称,但到目前为止,这确实是我代码的核心。只是给Toast打了几个电话,希望我能看到这件事是可行的。
我在这个主题上找到的所有教程都完全忽略了清单。昨晚我四处闲逛,试图调整一些明显的选项,以为这可能与此有关,但没有结果。无论如何,为了完整起见,以下是活动的清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.william.android.myAPI.tester"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyApiTester" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>以及服务:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.william.android.myAPI"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name=".MyApiService"></service>
</application>
</manifest>我尝试在<service>块中添加以下内容,但似乎没有帮助:
<intent-filter>
<action android:name="net.william.android.myAPI.MyApiService" />
</intent-filter>不管怎么说,这是我第一次发到你的论坛,所以我道歉如果有太多或太少的信息,或如果我错过了一些非常容易的修补在网上某处。我已经熬夜了大半个晚上,阅读了Google所有的服务文档,并在论坛上一次又一次地阅读文章,所以,在这一点上,我很容易忽略一些东西。提前感谢有时间帮忙的人!
发布于 2011-08-12 15:42:00
如果要将这些文件作为两个单独的APK文件分发,如您的清单所示,则不能使用以Java作为参数的Intent构造函数。这甚至表明您的项目设置有点可怕--从构建时的角度来看,您的客户不应该在您的服务的国家范围内。
将<intent-filter>添加到<service>广告中,说明您希望如何连接到,并在客户端中使用它创建的Intent结构。
<service android:name=".BshService">
<intent-filter>
<action android:name="com.commonsware.android.advservice.IScript" />
</intent-filter>
</service>因此,客户端使用:
new Intent("com.commonsware.android.advservice.IScript")https://stackoverflow.com/questions/7042005
复制相似问题