• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Android学习Android广播机制

android 搞代码 4年前 (2022-03-01) 22次浏览 已收录 0个评论

在Android中实现播送,首先咱们要在Manifest.xml文件中配置一个<receiver/>标签,这个标签必须有一个android:name属性,值为继承自BroadcastReceiver类的接收器类!这个标签还有一个子标签为<intent-filter/>,这个标签很重要,是指定接收器须要接管哪种播送。另外,还有配置一个用户权限:<uses-permission/>,具体的值能够参考官网API文档。
另外一个比拟重要的步骤是必须有一个类继承自BroadcastReceiver类,并复写onReceiver办法,在该办法中解决接管到播送后须要解决的事件!
上面来看一个具体的例子,有助于更好的了解播送机制是怎么一回事。
UI局部就不说了,Activity上就加了一个按钮,点击后发送播送。接收器接管到播送后在终端输入一句话。
首先看AndroidManifest.xml文件:
 `
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
            package=”com.gufengxiachen.broadcast”
            android:versionCode=”1″
            android:versionName=”1.0″>
        <uses-sdk android:minSdkVersion=”8″ />

        <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
                <activity android:name=”.BroadCast”
                                    android:label=”@string/app_name”>
                        <intent-filter>
                                <action android:name=”android.intent.action.MAIN” />
                                <category android:name=”android.intent.category.LAUNCHER” />
                        </intent-filter>
                </activity>

<receiver android:name=”.MyBroadCastReceiver”>
<intent-filter>
<action android:name=”android.intent.action.EDIT”></action>
</intent-filter>

</receiver>
        </application>
    
        <uses-permission android:name=”android.permission.RECEIVE_SMS”></uses-permission>
</manifest>

 `
上面是Activity:
 `
package com.gufengxiachen.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BroadCast extends Activity {
        /* Called when the activity is first created. /
private Button bound =null;
private Button unbound =null;
private Button sendBroadCast =null;
private SecondBroadCastReceiver sbr =null;

public static final String SMS_ACTION=”android.provider.Telephony.SMS_RECEVIER”;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                bound = (Button)findViewById(R.id.bound);
                unbound = (Button)findViewById(R.id.unbound);
                sendBroadCast = (Button)findViewById(R.id.sendBroadCast);
                bound.setOnClickListener(new BoundListener());
                unbound.setOnClickListener(new UnboundListener());
                sendBroadCast.setOnClickListener(new SendBroadCast());
        }
        class SendBroadCast implements OnClickListener{
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
    
        Intent intent = new Intent(Intent.ACTION_EDIT);
        BroadCast.this.sendBroadcast(intent);
        }
        }
        
        class BoundListener implements OnClickListener{
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        sbr = new SecondBroadCastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SMS_ACTION);
        BroadCast.this.registerReceiver(sbr, filter);        
        }
        }
        
class UnboundListener implements OnClickListener{
    
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        BroadCast.this.unregisterReceiver(sbr);
        }

        

        }
}

 
 `
最初是接收器类:
 `
package com.gufengxiachen.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadCastReceiver extends BroadcastReceiver{
public MyBroadCastReceiver(){

}
@Override
public void onReceive(Context context, Intent intent) {

System.out.println(“message receiver”);
}
}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Android学习Android广播机制

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址