Toast 和广播接收器不起作用
Posted
技术标签:
【中文标题】Toast 和广播接收器不起作用【英文标题】:Toast and Broadcast receiver doesn't work 【发布时间】:2021-12-13 18:11:30 【问题描述】:你好,当我点击按钮时我尝试了很多次它没有显示 toast 和广播接收器
附在下面的代码请纠正我的失误成功运行,但是当单击按钮时什么都没有发生,我尝试在 activity_main.xml 中进行编辑,但没有发生任何事情
MainActivity.java
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// broadcast a custom intent.
public void broadcastIntent(View view)
Intent intent = new Intent();
intent.setAction("com.myapplication.BOOT_COMPLETED"); sendBroadcast(intent);
MyReceiver.java
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.myapplication.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:text="Example of Broadcast"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_
android:layout_
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_
android:layout_
android:layout_centerInParent="true"
android:minWidth="48dp"
android:minHeight="48dp"
tools:ignore="SpeakableTextPresentCheck" />
<Button
android:id="@+id/button2"
android:layout_
android:layout_
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true"
android:onClick="broadcastIntent"
android:text="broadcast_intent" />
</RelativeLayout>
【问题讨论】:
【参考方案1】:希望对你有帮助
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity
BroadcastReceiver br;
IntentFilter filter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
br = new MyReceiver();
filter= new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
public void broadcastIntent(View view)
Intent intent = new Intent();
intent.setAction("com.myapplication.BOOT_COMPLETED");
sendBroadcast(intent);
this.registerReceiver(br, filter);
另见此链接 https://developer.android.com/guide/components/broadcasts
【讨论】:
【参考方案2】:您正在使用清单声明的接收器。此接收器会在其他应用程序外部或通过某些系统事件调用。
然后接收器成为您应用程序的单独入口点,这意味着如果应用程序当前未运行,系统可以启动应用程序并传送广播。
如果你想在应用内触发这个接收器,你应该注册这个接收器。在您的情况下,MainActivity
内的 onCreate
或 onResume
如下
broadcastReceiver = new MyReceiver ()
Intent filter = new IntentFilter("com.myapplication.BOOT")
registerReceiver(broadcastReceiver, filter)
您还需要在onDestroy
或onPause
中取消注册
unregisterReceiver(broadcastReceiver)
我猜你应该使用上下文注册接收器来处理你想要在应用内发送广播的用例。它几乎相同,只是您不需要在清单中定义它。
更多信息here
【讨论】:
以上是关于Toast 和广播接收器不起作用的主要内容,如果未能解决你的问题,请参考以下文章