Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 意向及;广播接收机_Android_Broadcastreceiver - Fatal编程技术网

Android 意向及;广播接收机

Android 意向及;广播接收机,android,broadcastreceiver,Android,Broadcastreceiver,我开始学习android开发,所以目前我的知识非常有限。 我试着玩广播,但我被卡住了,我不明白我做错了什么。 我想做的只是从广播接收器上简单地敬酒 该意图是清单中定义的自定义意图: <receiver android:name=".receiver.SendReceiver" android:enabled="true"> <intent-filter> <action android:name="com.android.terralink.sem.

我开始学习android开发,所以目前我的知识非常有限。 我试着玩广播,但我被卡住了,我不明白我做错了什么。 我想做的只是从广播接收器上简单地敬酒

该意图是清单中定义的自定义意图:

<receiver android:name=".receiver.SendReceiver" android:enabled="true">
     <intent-filter>
   <action android:name="com.android.terralink.sem.SOCCIA"></action>
     </intent-filter>
</receiver>
在应用程序调用的第一个操作中,我执行以下操作:

Intent i2 = new Intent(this, SearchReceiver.class);
i2.setAction(CUSTOM_INTENT);
sendBroadcast(i2);
我检查了Toast代码是否在活动中有效,但在广播中无效。 这是因为祝酒词不能在接收者上下文中显示

另外,还有一个关于android应用程序结构的问题。 如果从我的活动中,我允许用户在文本框中插入字符串并提交(按钮绑定),并且我希望应用程序在后台执行搜索,并在结果准备好后通知用户,是否正确执行以下操作? 1) 带搜索框的主要活动 2) 启动获取数据的服务,发送广播 3) 接收者对用户进行识别,并打开一个显示结果的活动

这样做有意义吗? 或者在完成其工作之前,通知应该由服务本身完成


谢谢

您不能在广播接收器中敬酒。 一旦onReceive()调用完成,Android通常会关闭您的进程(目前最多只允许花费10秒)。因为toast是异步显示的,所以我认为它的上下文在显示之前就被删除了

作为Toast的替代方案,您可以查看RemoveViews的概念,以便从接收方在另一个进程中更新UI。或者,启动一个敬酒并立即结束的活动


对于您的问题#2,我建议您在单击“搜索”按钮后继续运行“活动”,并启动搜索的异步任务,在搜索结果出现时更新搜索结果。

我在注册接收者时发现问题出现在清单中。 实际上,在听筒里敬酒是可能的,没有任何问题。
干杯

要通过广播接收器向观众敬酒,我们可以使用以下代码:

public class ServiceReceiver extends BroadcastReceiver {

    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        TelephonyManager telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    private class MyPhoneStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Toast.makeText(context, "Idle call", Toast.LENGTH_LONG).show();
                Log.d("***************************DEBUG", "IDLE");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("***************************DEBUG", "OFFHOOK");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Toast.makeText(context, "Ringing call", Toast.LENGTH_LONG)
                        .show();
                Log.d("***************************DEBUG", "RINGING");
                break;
            }
        }
    }
}

是的,有道理,我想发生的事情,只是不是100%确定。thx很多
public class ServiceReceiver extends BroadcastReceiver {

    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        TelephonyManager telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    private class MyPhoneStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Toast.makeText(context, "Idle call", Toast.LENGTH_LONG).show();
                Log.d("***************************DEBUG", "IDLE");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("***************************DEBUG", "OFFHOOK");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Toast.makeText(context, "Ringing call", Toast.LENGTH_LONG)
                        .show();
                Log.d("***************************DEBUG", "RINGING");
                break;
            }
        }
    }
}