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

相关内容

热门资讯

技术分享"优乐江西辅... 技术分享"优乐江西辅助器"开挂(平台)辅助平台有挂详细-AI教程 了解更多开挂安装加(1367043...
交流学习经验"哈糖大... 【亲,哈糖大菠萝提高胜率 这款游戏可以开挂的,确实是有挂的,很多玩家在这款哈糖大菠萝提高胜率中打牌都...
透视游戏"广东闲来辅... 透视游戏"广东闲来辅助软件官方"开挂(工具)辅助工具有挂技巧-力荐教程;无需打开直接搜索薇:1367...
揭秘几款"wepok... wepoker高级辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,wepoker高级辅助作为一种...
盘点十款"温州茶苑手... 盘点十款"温州茶苑手机版辅助器"开挂(神器)辅助神器了解有挂-2026新版教程1、下载安装好温州茶苑...
必备科技"多乐够级辅... 必备科技"多乐够级辅助软件"开挂(透视)辅助透视新版有挂-安装教程【无需打开直接搜索加薇136704...
透视好友"中至余干六... 透视好友"中至余干六副里"开挂(软件)辅助软件详细教程-攻略方法 了解更多开挂安装加(1367043...
实测发现“好运娱乐辅助器”固有... 实测发现“好运娱乐辅助器”固有有辅助开挂平台(有挂透明挂);无需打开直接搜索加(薇:13670430...
透视线上“逗娱碰胡开挂”起初有... 透视线上“逗娱碰胡开挂”起初有开挂辅助插件(有挂实锤);无需打开直接搜索加薇136704302(咨询...
八分钟辅助“约局吧能不能开挂”... 八分钟辅助“约局吧能不能开挂”开挂(平台)辅助平台科技教程-有挂教程 【无需打开直接搜索加薇1367...