重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

android无序广播发送与接收实例

郑州app开发android无序广播发送与接收实例。思路如下,需要建立两个工程。其中一个是发送无序广播,另外一个是接收广播。

创新互联建站-专业网站定制、快速模板网站建设、高性价比东城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式东城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖东城地区。费用合理售后完善,十多年实体公司更值得信赖。

发送广播的java代码

package cn.xhhkj.cyd;

import androidx.appcompat.app.AppCompatActivity;

import android.app.pendingIntent;

import android.content.broadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.Sharedpreferences;

import android.os.bundle;

import android.telephony.SmsManager;

import android.text.TextUtils;

import android.util.Log;

import android.view.View;

import android.widget.button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private button btn;

    @Override

    protected void onCreate(bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btn=findViewbyId(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent intent=new Intent();

                intent.setAction("cn.xhhkj.broadcast");

                intent.putExtra("key","hello");

                sendbroadcast(intent);

            }

        });

    }

}

接收无序广播的java代码

package cn.xhhkj.xhhkjtest;

import android.content.broadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

public class Mybroadcast extends broadcastReceiver {

    private static final String TAG = "MyReceiver";

    @Override

    public void onReceive(Context context, Intent intent) {

        Log.d(TAG,"接收到了"+intent.getStringExtra("key"));

    }

}

最后调试成功的代码

2020-02-18 13:04:38.669 2876-2876/cn.xhhkj.xhhkjtest D/MyReceiver: 接收到了hello


网页题目:android无序广播发送与接收实例
文章出自:http://cqcxhl.com/article/scciep.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP