如何在不绘制活动的情况下将意图传递给我的应用程序?

时间:2018-02-09 06:59:36

标签: android android-intent intentservice

我正在实施一个简单的应用程序,可以从YouTube应用程序接收“共享”并将其发送到服务器。

我可以将Intent传递给我已实施的ShareActivity,但每次我从YouTube分享视频时,都会打开ShareActivity

由于用户在共享时不需要与应用程序进行交互,因此我想完全删除此阶段。也就是说,我希望收到来自Youtube的Intent在后台处理,而不会给用户带来任何麻烦。

我正在考虑创建一个可以处理此类传入Intent的IntentService,但我不确定如何继续。 (这是实现此目的的正确方法吗?如果是这样,我应该从哪里开始IntentService?如果用户杀死我的应用程序,是否可以接收Intents?)

我似乎无法在互联网上找到任何关于此的资源。任何帮助表示赞赏。

编辑:好的,这是一个最小的,不工作的例子。我希望这可以帮助你们更好地指导我。因为我被卡住了。

Android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blumonkey.versatyl">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".ShareService"
        android:exported="true">

        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:host="www.youtube.com" android:mimeType="text/*" />
        </intent-filter>

    </service>
</application>

MainActivity.java

package com.blumonkey.versatyl;

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent msgIntent = new Intent(this, ShareService.class);
        startService(msgIntent);
    } 
}

ShareService.java

package com.blumonkey.versatyl;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;


/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p>
 * TODO: Customize class - update intent actions and extra parameters.
 */
public class ShareService extends IntentService {
    public ShareService() {
        super("ShareService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
            Log.d("Msg:", "Got Intent!");
    }
}

The <code>IntentChooser</code> that I see when I try to share a video on my AVD

我在日志中看到的唯一消息是当MainActivity将Intent发送给服务以启动它时。

1 个答案:

答案 0 :(得分:0)

如果您需要长时间使用资源,

IntentService会很好,甚至是常规Service - 您只需添加IntentFilter并使用正确的过滤器选项清单中的服务。

Android文档为ActivitiesService解释了它: https://developer.android.com/guide/components/intents-filters.html

有时Intent设计为由Activity处理,所以请小心谨慎。

如果用户“杀死”它,这是“唤醒”您的应用的正确方法。您的应用程序旨在处理的Intent将收到它。但是,有时用户可以将首选应用设置为处理某些Intent,在这种情况下,您无法控制发生的情况。