安卓接收不到发送的广播

qq_35709020 2018-04-09 09:29:26
已经静态注册了广播
在第一个activity中通过广播发送一个时间信号
在另一个activity中接收信号并显示在textview中

public void Sendtime(View view){
//使用广播发送时间
Intent intent=new Intent("android.sendtime");
sendBroadcast(intent, String.valueOf(A));
}

注册:

<receiver android:name=".Settings$TimeReceiver" >
<intent-filter>
<action android:name="android.sendtime"/>

</intent-filter> </receiver>


public class TimeReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
timeshow.setText(intent.getAction());
}
}
...全文
726 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxx355200 2018-04-11
  • 打赏
  • 举报
回复
这样发送消息:
 Intent intent = new Intent();
                intent.setAction("abc");
                intent.putExtra("bbb", "ccc");
                sendBroadcast(intent);
接收: Toast.makeText(context, intent.getStringExtra("bbb"), Toast.LENGTH_SHORT).show();
键盘舞者113 2018-04-09
  • 打赏
  • 举报
回复
又或者需要添加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.permissionbroadcastreceiver">
<uses-permission android:name="com.example.broadcast.permission" />
    <permission
        android:name="com.example.broadcast.permission"
        android:protectionLevel="normal" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".PermissionRecevicer"
            android:permission="com.example.broadcast.permission">
            <intent-filter>
                <action android:name="com.example.permissionbroadcastreceiver.message" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
键盘舞者113 2018-04-09
  • 打赏
  • 举报
回复
现在需要加上包名才行
Intent it = new Intent("com.hisense.test.broadcast");  
it.setComponent(new ComponentName("com.example.broadcasttest",  
                        "com.example.broadcasttest.MyBroadcast"));  
sendBroadcast(it);  

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧