Android11上的附近连接广告
创始人
2024-08-20 11:30:06
0

以下是使用 Android 11 上的 Nearby Connections 广告 API 的代码示例:

首先,为了使用此功能,您需要将以下依赖项添加到您的应用程序级别的 build.gradle 文件中:

dependencies {
    implementation 'com.google.android.gms:play-services-nearby:17.0.0'
}

在您的应用程序中,您需要初始化 Nearby Connections API。在 onCreate() 方法中添加以下代码:

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Nearby.CONNECTIONS_API)
        .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
            @Override
            public void onConnected(Bundle bundle) {}
        
            @Override
            public void onConnectionSuspended(int i) {}
        })
        .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
        })
        .build();

mGoogleApiClient.connect();

接下来,您需要为通过 Nearby Connections 广告服务进行通信的设备创建回调。在您的 Activity 或 Fragment 中实现以下回调:

private final ConnectionLifecycleCallback mConnectionLifecycleCallback =
        new ConnectionLifecycleCallback() {
            @Override
            public void onConnectionInitiated(
                    @NonNull String endpointId, @NonNull ConnectionInfo connectionInfo) {
                // 处理连接请求
                Nearby.getConnectionsClient(getContext()).acceptConnection(endpointId, mPayloadCallback);
            }

            @Override
            public void onConnectionResult(@NonNull String endpointId, @NonNull ConnectionResolution result) {}

            @Override
            public void onDisconnected(@NonNull String endpointId) {}
        };

然后,您需要创建一个用于呈现广告的服务,并初始化它:

AdvertisingOptions options = new AdvertisingOptions.Builder()
        .setStrategy(Strategy.P2P_STAR)
        .build();

Nearby.getConnectionsClient(this)
        .startAdvertising(
                /* endpointName= */ "EndpointName",
                /* serviceId= */ "com.example.app",
                mConnectionLifecycleCallback,
                options)
        .addOnSuccessListener(new OnSuccessListener() {
            @Override

相关内容

热门资讯

专业讨论!wepoker怎么发... 专业讨论!wepoker怎么发冤家牌(辅助挂)总是真的是有挂(果真有挂辅助教程)1、每一步都需要思考...
今日头条!微扑克数据分析(辅助... 今日头条!微扑克数据分析(辅助挂)果然是真的有挂(存在有挂辅助app)1)免费钻石:进一步探索免费脚...
必备攻略!约局吧德州透视(辅助... 必备攻略!约局吧德州透视(辅助挂)切实有挂(有挂规律辅助脚本)1、在插件功能辅助器技巧中,中转单元十...
总算清楚!wpk辅助(辅助挂)... 总算清楚!wpk辅助(辅助挂)其实真的是有挂(有挂方式辅助器)该软件可以轻松地帮助玩家将外卦神器提升...
程序员教你!wepoker私人... 程序员教你!wepoker私人局俱乐部(辅助挂)确实存在有挂(有挂分享辅助方法)1、脚本辅助下载、免...
分享个大家!wepoker免费... 分享个大家!wepoker免费脚本咨询(辅助挂)切实是有挂(有挂存在辅助工具)能透视中分为三种模型:...
玩家亲测!wepoker轻量版... 玩家亲测!wepoker轻量版透视系统(辅助挂)一贯是有挂(有挂方式辅助工具)玩家亲测!wepoke...
记者发布!aapoker怎么拿... 记者发布!aapoker怎么拿好牌(辅助挂)一贯真的有挂(有挂头条辅助软件)1、首先打开辅助器下载最...
分享认知!wepoker辅助分... 分享认知!wepoker辅助分析器(辅助挂)真是真的有挂(确实有挂辅助软件)1、用户打开应用后不用登...
重大通报!wepoker手机版... 重大通报!wepoker手机版透视脚本(辅助挂)一直存在有挂(有挂神器辅助教程)1、上手简单,内置详...