简体   繁体   English

没有GUI的Android Activity

[英]Android Activity with no 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. 我创建了一个仅用于从链接启动的活动(使用intent过滤器。)我不希望这个活动有一个GUI - 我只是想让它启动一个服务并在栏中发出通知。 I have tried to put the intent filter for the link in my service, but that does not work. 我试图在我的服务中为链接设置intent过滤器,但这不起作用。 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? 有没有更好的方法来回答意图过滤器 - 或者我可以让我的活动没有GUI吗?
Sorry if I'm being confusing, Isaac 对不起,如果我困惑,艾萨克

Echoing previous response, you shouldn't use a broadcast receiver. 与先前的响应相呼应,您不应使用广播接收器。

In the same situation, what I did was to declare the theme thusly: 在同样的情况下,我所做的就是如此宣布主题:

<activity android:name="MyActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoDisplay">

Your best bet would seem to be using a BroadcastReceiver . 你最好的选择似乎是使用BroadcastReceiver You can create a new BroadcastReceiver that listens for the Intent to trigger your notification and start your service like this: 您可以创建一个新的BroadcastReceiver来侦听Intent以触发您的通知并启动您的服务,如下所示:

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));
    }
  }    
}

And you can register this IntentReceiver directly in the application Manifest without needing to include it within an Activity: 您可以直接在应用程序清单中注册此IntentReceiver,而无需将其包含在活动中:

<?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> 

I'm not sure if a service would work, but a broadcast receiver definitely would not. 我不确定服务是否可行,但广播接收机肯定不会。 Url's are launched using startActivity(). Url是使用startActivity()启动的。 Broadcast receivers cannot respond to this. 广播接收器无法对此做出响应。

http://developer.android.com/reference/android/content/BroadcastReceiver.html http://developer.android.com/reference/android/content/BroadcastReceiver.html

FTA: Note that, although the Intent class is used for sending and receiving these broadcasts, the Intent broadcast mechanism here is completely separate from Intents that are used to start Activities with Context.startActivity(). FTA:请注意,虽然Intent类用于发送和接收这些广播,但此处的Intent广播机制与用于使用Context.startActivity()启动活动的Intent完全分开。 There is no way for a BroadcastReceiver to see or capture Intents used with startActivity(); BroadcastReceiver无法查看或捕获与startActivity()一起使用的Intent; likewise, when you broadcast an Intent, you will never find or start an Activity. 同样,当你广播一个意图时,你永远不会找到或开始一个活动。

Use Service. 使用服务。 I works definitely. 我肯定工作。 When you click the program, it would do its work without any GUI. 当您单击该程序时,它将在没有任何GUI的情况下完成其工作。 Use pendintgintent...getService(MySerice.class....). 使用pendintgintent ... getService(MySerice.class ....)。 Then, create a new class MyService extending the Service class. 然后,创建一个扩展Service类的新类MyService。 Inside MyService.class, override onStart() and do whatever you want to do. 在MyService.class中,覆盖onStart()并执行您想要执行的任何操作。

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

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