没有 GUI 的 Android Activity
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/526074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Activity with no GUI
提问by Isaac Waller
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
我创建了一个仅打算从链接启动的活动(使用意图过滤器)。我不希望此活动具有 GUI - 我只希望它启动服务并在栏中放置通知。我试图将链接的意图过滤器放在我的服务中,但这不起作用。有没有更好的方法可以回答意图过滤器 - 或者我可以让我的活动没有 GUI 吗?
对不起,如果我感到困惑,艾萨克
采纳答案by Reto Meier
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:
您最好的选择似乎是使用BroadcastReceiver
. 您可以创建一个新的 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,而无需将其包含在 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>
回答by JoeHz
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">
回答by lukejduncan
I'm not sure if a service would work, but a broadcast receiver definitely would not. Url's are launched using startActivity(). Broadcast receivers cannot respond to this.
我不确定服务是否有效,但广播接收器肯定不会。Url 是使用 startActivity() 启动的。广播接收器无法对此做出响应。
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(). There is no way for a BroadcastReceiver to see or capture Intents used with startActivity(); likewise, when you broadcast an Intent, you will never find or start an Activity.
FTA:注意,虽然Intent类用于发送和接收这些广播,但这里的Intent广播机制与通过Context.startActivity()启动Activity的Intent是完全分开的。BroadcastReceiver 无法查看或捕获与 startActivity() 一起使用的 Intent;同样,当你广播一个 Intent 时,你永远不会找到或启动一个 Activity。
回答by Manoj
Use Service. I works definitely. When you click the program, it would do its work without any GUI. Use pendintgintent...getService(MySerice.class....). Then, create a new class MyService extending the Service class. Inside MyService.class, override onStart() and do whatever you want to do.
使用服务。我肯定工作。当您单击该程序时,它会在没有任何 GUI 的情况下完成其工作。使用 pendintgintent...getService(MySerice.class....)。然后,创建一个扩展 Service 类的新类 MyService。在 MyService.class 中,覆盖 onStart() 并做任何你想做的事情。