通过快捷方式运行后台服务

时间:2014-04-10 10:42:41

标签: java android android-service

所以我想从快捷方式启动服务。我知道这是不可能直接做的,所以我设置了一个活动,其唯一目的就是启动服务。 我的服务的目的是发送一个意图到另一个应用程序,然后5秒后发送另一个,所以我使用CountDownTimer来做到这一点。 但是,当我启动从快捷方式启动服务的Activity时(这会让人感到困惑),它会启动应用程序UI。我不希望这样,因为我希望它成为后台服务。 我究竟做错了什么。我刚刚进入开发阶段,所以它可能是显而易见的,但我现在已经和它斗争了几天了。

出于某种原因,当我从服务中运行它时,它会立即启动应用程序... 当我直接从不可见的活动中运行它,它正常运行第5秒,然后加载应用程序... 我无法弄清楚为什么它会加载应用程序。

我已尽可能多地提供相关的信息。 任何帮助表示赞赏!

我的服务:

    public class Pop1_5Service extends IntentService {

    public Pop1_5Service() {
        super("Pop1_5Service");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // Normally we would do some work here, like download a file.
        // For our sample, we just sleep for 5 seconds.
        new CountDownTimer(5000, 2500) {
            public void onTick(long millisUntilFinished) {
                Intent i = new Intent(INTENT_ACTION);
                Bundle b = new Bundle();
                b.putInt(BUNDLE_VERSION_CODE, 1);
                b.putString(BUNDLE_STRING_NAME, "POP1");
                b.putString(BUNDLE_STRING_VALUE, "1");
                i.putExtra(BUNDLE_NAME, b);
                sendBroadcast(i);           }
            public void onFinish() {
                Intent i = new Intent(INTENT_ACTION);
                Bundle b = new Bundle();
                b.putInt(BUNDLE_VERSION_CODE, 1);
                b.putString(BUNDLE_STRING_NAME, "POP1");
                b.putString(BUNDLE_STRING_VALUE, "1");
                i.putExtra(BUNDLE_NAME, b);
                sendBroadcast(i);           }
            }
        }.start();
    }
}

启动服务的活动:

public class Pop1_5Activity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);     

    Intent intent = new Intent(this, Pop1_5Service.class);
        startService(intent);
        finish();
    }
}

清单的分节:

<activity 
    android:name=".Pop1_5Activity"
    android:theme="@android:style/Theme.NoDisplay">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<service android:name=".Pop1_5Service" />

“创建快捷方式”活动:

public class CreateShortcutActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent shortcutintent = new Intent(this, Pop1_5Activity.class);
        ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutintent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Pop1_5");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        setResult(RESULT_OK, intent);
        finish();
    }   
}

1 个答案:

答案 0 :(得分:0)

从外观上看,看起来CreateShortcutActivity什么都不做。 您的LAUNCHER是Pop1_5Activity,因此当用户按下应用程序图标时,此活动将运行,并启动服务。

您向我们展示的所有代码都是“隐形”,两个活动自行完成(),服务是服务。

您可能想看看BroadcastReceiver如何处理您的广播。例如,它是否通过PendingIntent创建另一个Activity?活动是否隐藏了?

也许您应该尝试在BroadcastReceiver中创建待处理的服务而不是待处理的活动。