Toast 和广播接收器不起作用 [英] Toast and Broadcast receiver doesn't work

查看:34
本文介绍了Toast 和广播接收器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我尝试了很多次,当我点击按钮时,它没有显示 Toast 和广播接收器

Hello i tried many times when i click the button it doesn't show the toast and broadcast receiver

下面附上的代码请纠正我的失误使其运行成功,但是当单击按钮时什么也没发生,我尝试在activity_main.xml中进行编辑,但什么也没发生

the code attached down there kindly correct my miss takes its run successfully but when click the button nothing happend at all , I tried to edit in activity_main.xml but nothing happend

MainActivity.java

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

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

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

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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Example of Broadcast"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:minWidth="48dp"
        android:minHeight="48dp"
        tools:ignore="SpeakableTextPresentCheck" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton"
        android:layout_centerHorizontal="true"
        android:onClick="broadcastIntent"
        android:text="broadcast_intent" />

</RelativeLayout>

推荐答案

您正在使用 清单声明的接收器.此接收器会从其他应用程序或某些系统事件外部调用.

You are using a Manifest-declared receiver. This receiver would get invoked outside from other app or with some system events.

接收器随后成为您的应用程序的单独入口点,这意味着系统可以启动应用程序并在应用程序当前未运行时传送广播.

The receiver then becomes a separate entry point into your app which means that the system can start the app and deliver the broadcast if the app is not currently running.

如果您想在应用内触发此接收器,则应注册此接收器.在您的情况下,onCreateonResume 内的 MainActivity 如下

If you want to trigger this receiver within the app you should register this receiver. In your case inside the MainActivity within onCreate or onResume as follows

broadcastReceiver = new MyReceiver ()
Intent filter = new IntentFilter("com.myapplication.BOOT")
registerReceiver(broadcastReceiver, filter)

您还需要在 onDestroyonPause

unregisterReceiver(broadcastReceiver)

我想您应该将上下文注册接收器用于您想要在应用内发送广播的用例.除了您不需要在清单中定义它之外,它几乎相同.

I guess you should use Context-registered receiver for your use case where you want to send a broadcast within the app. Its almost the same except that you don't need to define it inside the manifest.

更多信息此处

这篇关于Toast 和广播接收器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆