我已经创建了一个仅打算从一个链接展开了活动,我不希望这个活动有一个GUI(使用意图过滤器) - 我只是想它来启动服务,并把通知中酒吧。我试图把意图过滤器在我服务的链接,但不起作用。有没有更好的事情要做到这一点,将回答的意图过滤器 - ?或者我可以让我的活动没有GUI结果 很抱歉,如果我被混淆,以撒

有帮助吗?

解决方案

您最好的选择似乎使用BroadcastReceiver是。您可以创建一个新的广播接收器监听的意图来触发您的通知,并开始为您服务是这样的:

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直接在应用程序清单中,而无需一个活动内包括它寄存器:

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

其他提示

呼应前面的反应,你不应该使用的广播接收机。

在相同的情况下,我所做的就是正是如此声明的主题:

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

我不知道,如果一个服务是可行的,但广播接收器绝对不会。网址的使用startActivity()启动。广播接收器不能对此作出回应。

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

FTA: 需要注意的是,虽然Intent类用于发送和接收这些广播,这里的意图广播机制是从被用来启动与Context.startActivity活动意图完全独立的()。有没有办法让一个BroadcastReceiver查看或使用startActivity使用捕捉意图();同样,当你播放的意图,你将永远找不到或启动活动。

使用服务。我的作品肯定。当您单击该程序,它会做的工作没有任何GUI。使用pendintgintent ...的getService(MySerice.class ....)。然后,创建扩展服务类的新类为MyService。里面MyService.class,覆盖在onStart()和做任何你想做的事情。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top