Android的活动,没有GUIAndroid、GUI

2023-09-12 11:10:14 作者:閑人到人间

我已经创建了只打算从一个链接展开了活动,我不希望这个活动有一个GUI(使用意图过滤器。) - 我只是希望它启动服务,并把通知中的酒吧。我试图把意图过滤器在我服务的链接,但不起作用。有没有更好的事情要做到这一点,将回答意图过滤器 - ?或者我只是让我的行为没有GUI 很抱歉,如果我变得扑朔迷离,艾萨克

I have created a activity that is only meant to be launched from a link (using a intent filter.) I do not want this activity to have a GUI - I just want it to start a service and put a notification in the bar. I have tried to put the intent filter for the link in my service, but that does not work. Is there a better thing to do this that will answer to intent filters - or can I just make my activity not have a GUI? Sorry if I'm being confusing, Isaac

推荐答案

您最好的选择似乎可以用一个的BroadcastReceiver 。您可以创建一个新的BroadcastReceiver侦听意图触发您的通知,并开始你的服务是这样的:

Your best bet would seem to be using a BroadcastReceiver. You can create a new BroadcastReceiver that listens for the Intent to trigger your notification and start your service like this:

public class MyIntentReceiver extends BroadcastReceiver {    
  @Override 
  public void onReceive(Context _context, Intent _intent) {
    if (_intent.getAction().equals(MY_INTENT)) {
      // TODO Broadcast a notification
      _context.startService(new Intent(_context, MyService.class));
    }
  }    
}

和您可以直接在应用程序清单,而无需一个活动中包括其注册此IntentReceiver:

And you can register this IntentReceiver directly in the application Manifest without needing to include it within an Activity:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.domain.myapplication">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <service android:enabled="true" android:name="MyService"></service>
    <receiver android:enabled="true" android:name="MyIntentReceiver">
      <intent-filter>
        <action android:name="MY_INTENT" />
      </intent-filter>
    </receiver>
  </application>
</manifest>