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私人局辅助,一贯存在有挂(有挂分享)1、下载好透视辅助下...
推荐十款!wepoker到底有... 推荐十款!wepoker到底有没有透视,aapoker辅助软件合法吗,果然真的是有挂(有挂方法)1、...
盘点一款!购买的wpk辅助在哪... 盘点一款!购买的wpk辅助在哪里下载,wejoker辅助软件视频,本来真的是有挂(揭秘有挂)1)免费...
揭秘一下!we poker辅助... 揭秘一下!we poker辅助器,wepoker怎么发冤家牌,切实存在有挂(有挂技巧)1、每一步都需...
一分钟揭秘!wepoker数据... 一分钟揭秘!wepoker数据分析工具,wepoker怎么买辅助,竟然真的是有挂(有挂方略)透视方法...
研究成果!wepoker私人局... 研究成果!wepoker私人局俱乐部辅助,wepoker免费脚本弱密码,一贯真的有挂(有挂技巧)1、...
普及知识!aa poker辅助... 普及知识!aa poker辅助包,wepoker怎么看底牌,总是真的有挂(有挂细节)1、脚本辅助下载...
重大消息!wpk有那种辅助吗,... 重大消息!wpk有那种辅助吗,hhpoker有没有外挂,确实存在有挂(有挂解惑)1、让任何用户在无需...
推荐攻略!wepoker有辅助... 推荐攻略!wepoker有辅助吗,hhpoker有后台操作吗,原来是真的有挂(有挂规律)1、让任何用...
技术分享!wepoker辅助器... 技术分享!wepoker辅助器激活码,wepokerplus到底是挂了吗,总是是真的有挂(有挂头条)...