简体   繁体   English

Android从接收器到活动

[英]Android from receiver to activity

I'm just getting into Android development, and I have a question about communicating between a receiver class and an activity class. 我刚刚进入Android开发,并且对接收器类和活动类之间的通信存在疑问。 I'm very new to JAVA and Android so I hope I don't sound too stupid. 我对JAVA和Android还是很陌生,所以我希望我不要听起来太愚蠢。 I'm developing an application where I intercept an SMS message and then based on various elements of that SMS I might delete it once it's been saved to the inbox. 我正在开发一个应用程序,在其中我会截取一条SMS消息,然后基于该SMS的各种元素,一旦将其保存到收件箱中,我可能会删除它。 I have a receiver class that intercepts the txt message, and I am also able to delete messages from my inbox with code in the activity class using a button at the moment. 我有一个接收器类,它拦截txt消息,并且现在还可以使用按钮使用活动类中的代码从收件箱中删除消息。 The problem I have is communicating between the receiver class and the activity class where the code to delete a message resides. 我遇到的问题是在删除消息的代码所在的接收方类和活动类之间进行通信。 I tried putting that code directly into the receiver class but as I'm sure most of you already know the BroadcastReceiver class doesn't seem to support what I need to delete messages. 我尝试将代码直接放入接收器类中,但是我确定大多数人已经知道BroadcastReceiver类似乎不支持我删除消息所需的内容。 I've been searching for an answer to this for a while, but haven't been able to find anything. 我已经在寻找答案了一段时间,但是却找不到任何东西。 Honestly I'm not sure I know enough about JAVA and Android to even recognize a solution if I saw it. 老实说,我不确定我对JAVA和Android的了解是否足够,即使我看到了解决方案,也无法识别。

You could implement the handling messages logic using an IntentService. 您可以使用IntentService实现处理消息逻辑。 When your receiver gets the new incomming message, start the IntentService passing an intent with the message data. 当您的接收者收到新的传入消息时,启动IntentService,以传递带有消息数据的意图。

Receiver 接收者

onReceive(Context context, Intent intent) {
    //Setup Intent
    Intent i = new Intent(context, MyIntentService.class);
    i.setAction(MyIntentService.HANDLE_MESSAGE);        
    //Pass data to intent
    i.putExtra(MyIntentService.MESSAGE_DATA, data);
    //Start Intent Service          
    context.startService(i);
}

MyIntentService MyIntentService

onHandleIntent(Intent i){
    String action = i.getAction();
    if(action != null && action.equals(MyIntentService.HANDLE_MESSAGE){
        //Get data and implement message logic
    }
}

Hope it helps. 希望能帮助到你。

If you need to complete a job without an interface look into creating a Service , if you need user interface just start an Activity 如果您需要在没有界面的情况下完成一项工作,请考虑创建Service ,如果您需要用户界面,只需启动一个Activity

You can use the Context parameter of the onReceive method of the receiver to start a new service/activity 您可以使用接收者的onReceive方法的Context参数来启动新的服务/活动

You can use Extras to pass params between context. 您可以使用Extras在上下文之间传递参数。 So you can put as extra the message id or entire message and pass it to your service/activity and deal it there. 因此,您可以额外添加消息ID或整个消息,然后将其传递给您的服务/活动并在那里进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM