我找不到任何关于如何在活动和服务之间发送消息的示例,我花了太多时间来搞清楚这一点.这是一个供其他人参考的示例项目.
此示例允许您直接启动或停止服务,并单独绑定/取消绑定服务.当服务运行时,它会以10 Hz的频率递增一个数字.如果活动绑定到Service,则会显示当前值.数据作为整数和字符串传输,因此您可以看到如何以两种不同的方式进行操作.活动中还有按钮将消息发送到服务(更改增量值).
截图:

AndroidManifest.xml中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampleservice"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"></service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Run Code Online (Sandbox Code Playgroud)
水库\值\ strings.xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ExampleService</string>
<string name="service_started">Example Service started</string>
<string name="service_label">Example Service Label</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
水库\布局\ main.xml中:
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service" >
</Button>
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Stop Service" >
</Button>
</RelativeLayout>
<RelativeLayout …Run Code Online (Sandbox Code Playgroud) 我目前在Android中有一个服务,它是一个示例VOIP客户端,因此它会侦听SIP消息,如果它收到一个,它会启动一个带有UI组件的活动屏幕.
然后,以下SIP消息确定要在屏幕上显示活动的内容.例如,如果它的来电将显示接听或拒绝或拨出电话,它将显示拨号屏幕.
在那一刻,我使用Intents让Activity知道它应该显示的状态.
一个例子如下:
Intent i = new Intent();
i.setAction(SIPEngine.SIP_TRYING_INTENT);
i.putExtra("com.net.INCOMING", true);
sendBroadcast(i);
Intent x = new Intent();
x.setAction(CallManager.SIP_INCOMING_CALL_INTENT);
sendBroadcast(x);
Log.d("INTENT SENT", "INTENT SENT INCOMING CALL AFTER PROCESSINVITE");
Run Code Online (Sandbox Code Playgroud)
因此,活动将为这些意图注册广播接收器,并根据收到的最后意图切换其状态.
示例代码如下:
SipCallListener = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(SIPEngine.SIP_RINGING_INTENT.equals(action)){
Log.d("cda ", "Got RINGING action SIPENGINE");
ringingSetup();
}
if(CallManager.SIP_INCOMING_CALL_INTENT.equals(action)){
Log.d("cda ", "Got PHONE RINGING action");
incomingCallSetup();
}
}
};
IntentFilter filter = new IntentFilter(CallManager.SIP_INCOMING_CALL_INTENT);
filter.addAction(CallManager.SIP_RINGING_CALL_INTENT);
registerReceiver(SipCallListener, filter);
Run Code Online (Sandbox Code Playgroud)
这可行,但它似乎不是非常有效,Intents将获得广播系统范围和Intents必须为不同状态触发似乎它可能变得低效,我必须包括更多,以及增加复杂性.
所以我想知道是否有更高效,更清洁的方法来做到这一点?
有没有办法让Intents只在应用程序内部广播?
回调是一个更好的主意吗?如果是这样,为什么以及以何种方式实施它们?