gpt4 book ai didi

android - 为什么两个 Activity 之间没有收到广播?

转载 作者:行者123 更新时间:2023-11-29 01:39:45 25 4
gpt4 key购买 nike

我不知道为什么我的接收器没有收到任何东西。我想在两个 Activity 之间发送广播。tv1 不显示“消息”。

这是代码

发送:

Intent intent = new Intent(action);     
intent.putExtra("msg", "a");
sendBroadcast(intent);
startActivity(new Intent(MainActivity.this,sec.class));

接收者:

IntentFilter intentFilter = new IntentFilter(MainActivity.action);

registerReceiver(receiver, intentFilter);

BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
tv1.setText(intent.getExtras().getString("msg"));
}
};

最佳答案

你可以这样实现:

 package com.example.broadcastrecieverdemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
private InternalReciever internalReciever;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
internalReciever = new InternalReciever();
IntentFilter filter = new IntentFilter("kishan");
registerReceiver(internalReciever, filter);
findViewById(R.id.btnSendBroadcast).setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.broadcastrecieverdemo.CUSTOM_INTENT");
sendBroadcast(intent);
}
});

findViewById(R.id.btnInternalSendBroadcast).setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("kishan");
sendBroadcast(intent);
}
});
}



@Override
protected void onDestroy() {
unregisterReceiver(internalReciever);
super.onDestroy();
}

}

class InternalReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Inernal Broadcast recieved",
Toast.LENGTH_SHORT).show();
}

}

Reciever.java

    package com.example.broadcastrecieverdemo;

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

public class Reciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast recieved", Toast.LENGTH_SHORT)
.show();
}
}

list

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcastrecieverdemo.MainActivity"
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=".Reciever" >
<intent-filter>
<action android:name="com.example.broadcastrecieverdemo.CUSTOM_INTENT" >
</action>
</intent-filter>
</receiver>
</application>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Broad cast receiver"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/btnSendBroadcast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="21dp"
android:gravity="center"
android:text="Send" />

<Button
android:id="@+id/btnInternalSendBroadcast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/btnSendBroadcast"
android:layout_marginTop="21dp"
android:gravity="center"
android:text="Send Internal" />

关于android - 为什么两个 Activity 之间没有收到广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25480046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com