小编Lan*_*ure的帖子

示例:使用Messaging在活动和服务之间进行通信

我找不到任何关于如何在活动和服务之间发送消息的示例,我花了太多时间来搞清楚这一点.这是一个供其他人参考的示例项目.

此示例允许您直接启动或停止服务,并单独绑定/取消绑定服务.当服务运行时,它会以10 Hz的频率递增一个数字.如果活动绑定到Service,则会显示当前值.数据作为整数和字符串传输,因此您可以看到如何以两种不同的方式进行操作.活动中还有按钮将消息发送到服务(更改增量值).

截图:

Android服务消息示例的屏幕截图

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 android-service android-activity

580
推荐指数
5
解决办法
28万
查看次数

示例:使用AsyncTask的Android双向网络套接字

我在Android上找到的大多数网络套接字示例都是单向的.我需要一个双向数据流的解决方案.我最终了解到了AsyncTask.此示例显示如何从套接字获取数据并将数据发送回它.由于正在接收数据的套接字的阻塞性质,阻塞需要在UI线程以外的线程中运行.

为了举例,此代码连接到Web服务器.按"Start AsyncTask"按钮将打开套接字.套接字打开后,Web服务器将等待请求.按"发送消息"按钮将向服务器发送请求.服务器的任何响应都将显示在TextView中.对于http,一旦发送了所有数据,Web服务器将断开与客户端的连接.对于其他TCP数据流,连接将一直保持到一侧断开连接.

截图:

应用程序截图

AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.exampleasynctask"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

水库\布局\ main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start AsyncTask"></Button>
<Button android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Message"></Button>
<TextView android:id="@+id/textStatus" android:textSize="24sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Status Goes Here" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

SRC\com.exampleasynctask\MainActivity.java:

package com.exampleasynctask;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress; …
Run Code Online (Sandbox Code Playgroud)

android android-networking android-asynctask

37
推荐指数
2
解决办法
9万
查看次数

如何从线程中的套接字获取数据?

Android noob在这里.通过查看功能示例的源代码,我学到了最好的东西,但是我无法找到在自己的线程中使用套接字的简单但完整的示例.

我有一个需要与互联网通信的Android服务.我想打开连接到Internet上的服务器的TCP套接字.该服务需要将数据发送到Internet,并且从网络返回的数据将需要转到该服务.由于服务也在做其他事情,因此套接字连接需要存在于自己的线程中.

知道我在哪里可以找到一个与套接字通信的线程中的套接字示例?

谢谢

在此输入图像描述

sockets multithreading android

9
推荐指数
2
解决办法
3万
查看次数

Android服务到活动的通信性能

我可以找到几个如何构建服务的示例,但是我很难找到如何在Activity和Service之间发送消息的工作示例.根据我的发现,我认为我的选择是使用Intents,AIDL,或者根据这个问题使用服务对象本身.

就我而言,我的活动是唯一可以访问该服务的活动,因此本地服务就可以.当活动打开时,我希望看到来自服务的一些状态消息,这些消息将以高达20 Hz的频率进入.这些通信方法每秒支持的消息数量是否有限制?基本上,哪种方法最适合我的情况?

谢谢.

android

5
推荐指数
1
解决办法
3391
查看次数