意图过滤器对他们的BroadcastReceiver? [英] BroadcastReceiver with intent-filter for them?

查看:120
本文介绍了意图过滤器对他们的BroadcastReceiver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

她就是我要完成的:

  1. 当用户试图从共享的任何应用程序的一些文字(像共享鸣叫或链接),我的应用程序将出现在共享列表中。
  2. 如果他选择我的应用程序,一些简单的code将运行(如呈现出吐司),那么仅此而已。无接口或UI是必要的。

下面是我做的:

AndroidManifest.xml中

 <接收器
机器人:MyBroadcastReceiverNAME =>
    <意图过滤器
        机器人:标签=选择我的应用程序>
        <作用机器人:名称=android.intent.action.SEND/>
        <类机器人:名称=android.intent.category.DEFAULT/>
        <数据机器人:MIMETYPE =text / plain的/>
    &所述; /意图滤光器>
< /接收器>
 

MyBroadcastReceiver.java

 公共无效的onReceive(背景为arg0,意图ARG1){
    // TODO自动生成方法存根
    //一些简单的code
    Toast.makeText(arg0中,我的收款人是酷,Toast.LENGTH_LONG).show();
}
 

这没有工作,我的应用程序不会在共享列表中显示。

为什么我用 BroadcatReceivers 工作,不与活动因为我不吨要任何UI,我认为这就是为什么我们有接收器(纠正我的PLZ)

我这样做对吗?

解决方案
  

这没有工作,我的应用程序不会在共享列表中显示。

ACTION_SEND 用于与活动。您正在尝试使用的BroadcastReceiver 。由于的BroadcastReceiver 不是一个活动,这是不行的。

  

我这样做对吗?

没有。

  

如果他选择我的应用程序,一些简单的code将运行(如呈现出吐司),那么仅此而已。无接口或UI是必要的。

您还需要一个活动。但是,如果你给的活动 Theme.NoDisplay ,你不叫的setContentView(),将不会有用户界面的活性。通常情况下,你只需要调用完成()的onCreate()做什么是你想做的事(在此之后,情况下,显示吐司)。

例如,这个示例应用程序展示活动( FauxSender )设置做$ P $你想pcisely什么:以吐司回应 ACTION_SEND 。它有发出 ACTION_SEND 这样你就可以看到结果。

另一个活动

Her is what I'm trying to accomplish:

  1. When user tries to share some text from any app (Like sharing a tweet or a link), my app will appear in the sharing list.
  2. If he select my app, some simple code will be run (like showing a Toast) then that's it. No interface or UI is needed.

Here is how I did it:

AndroidManifest.xml

<receiver 
android:name=".MyBroadcastReceiver" >
    <intent-filter
        android:label="select my app">
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>                
</receiver>

MyBroadcastReceiver.java

public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    //Some simple code
    Toast.makeText(arg0, "My Receiver is cool", Toast.LENGTH_LONG).show();
}

This didn't work, my app doesn't show in the sharing list.

Why I work with BroadcatReceivers and not with Activities? Because I don't want any UI and I think that is why we have receivers (correct my plz)

Am I doing it right?

解决方案

This didn't work, my app doesn't show in the sharing list.

ACTION_SEND is used with activities. You are attempting to use a BroadcastReceiver. Since a BroadcastReceiver is not an activity, this will not work.

Am I doing it right?

No.

If he select my app, some simple code will be run (like showing a Toast) then that's it. No interface or UI is needed.

You still need an activity. However, if you give the activity Theme.NoDisplay, and you do not call setContentView(), there will be no UI for the activity. Usually, you just call finish() from onCreate() after doing whatever it is you wanted to do (in this case, display a Toast).

For example, this sample application shows an activity (FauxSender) set up to do precisely what you want: respond to ACTION_SEND with a Toast. It has another activity that issues an ACTION_SEND so you can see the results.

这篇关于意图过滤器对他们的BroadcastReceiver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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