从其他应用启动IntentService



我试图从其他应用程序启动IntentService,现在我有一个Activity与AsyncTask,我下载一个文件。这工作得很好,但我想在一个IntentService中下载文件,不需要显示活动,只是一个通知。这可能吗?我读到IntentService开始时,一个活动调用它通过startService(Intent),但当我尝试什么都没有发生:(

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bajar);
    intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    if(Intent.ACTION_SEND.equals(action) && type != null){
        if("text/plain".equals(type)){
            String todo = intent.getStringExtra(Intent.EXTRA_TEXT);
            if(todo != null){
                id = new Descarga(todo);
                EditText textNombre = (EditText) findViewById(R.id.TextNombre);
                textNombre.setText(id.nombre);
                /*
                This works
                d = new Download();
                d.execute();
                /*
                startService(intent); //This don´t work
            }
        }
    }
}

活动xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="es.esy.palmaseca.descargayt.Bajar">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/nombre"
    android:id="@+id/txtNombre"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center" />
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/TextNombre"
    android:layout_below="@+id/txtNombre"
    android:layout_centerHorizontal="true"
    android:editable="true" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:id="@+id/txtActual"
    android:layout_marginTop="44dp"
    android:layout_below="@+id/TextNombre"
    android:layout_centerHorizontal="true" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:id="@+id/txtFinal"
    android:layout_below="@+id/txtActual"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="35dp" />
<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/barra"
    android:layout_below="@+id/txtFinal"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="55dp" />

我的类IntentService:

public class Descargar extends IntentService {
NotificationManager nm;
Notification notification;
static String ns = Context.NOTIFICATION_SERVICE;
int icono = R.drawable.icono;
public Descargar() {
    super("Descargar");
    Log.d("Serivicio", "Star");
}
@Override
protected void onHandleIntent(Intent intent) {

    String action = intent.getAction();
    String type = intent.getType();
    if(Intent.ACTION_SEND.equals(action) && type != null){
        if("text/plain".equals(type)){
            String todo = intent.getStringExtra(Intent.EXTRA_TEXT);
            if(todo != null){
                Descarga d = new Descarga(todo);
                bajar(d);
            }
        }
    }
}

函数bajar与Download AsynTask相同。

最后是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="es.esy.palmaseca.descarga" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@drawable/icono"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:enabled="true"
        android:name=".Descargar"
        android:exported="true" >
    </service>
    <activity
        android:name=".Bajar"
        android:label="@string/bajar" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
</application>

我已经尝试启动IntentService,但我找不到错误在哪里,并抛出0错误

对不起,我的英文🙏

一个简单的解决方案是创建一个BroadcastReceiver,在你的manifest中注册它,然后让它启动intentservice。

public class Descargar extends IntentService {
NotificationManager nm;
Notification notification;
static String ns = Context.NOTIFICATION_SERVICE;
int icono = R.drawable.icono;
public static class DescargarReceiver implements BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(), Descargar.class.getName());
    context.startService(intent.setComponent(comp));
    }
}

public Descargar() {
    super("Descargar");
    Log.d("Serivicio", "Star");
}
@Override
protected void onHandleIntent(Intent intent) {
    String action = intent.getAction();
    String type = intent.getType();
    if(Intent.ACTION_SEND.equals(action) && type != null){
        if("text/plain".equals(type)){
            String todo = intent.getStringExtra(Intent.EXTRA_TEXT);
            if(todo != null){
                Descarga d = new Descarga(todo);
                bajar(d);
            }
        }
    }
}

和manifest:

<receiver
    android:name=".Descargar$DescargarReceiver"
    android:exported="true>
    <intent-filter>
        <action
            android:name="com.mycompany.action.START_DESCARGAR_SERVICE" />
    </intent-filter>
</receiver>

    <service
        android:enabled="true"
        android:name=".Descargar"
        android:exported="true" >
    </service>

然后你就可以广播任何应用的意图了:

Intent intent = new Intent("com.mycompany.action.START_DESCARGAR_SERVICE");
intent.putExtra(.....)
sendBroadcast(intent);

最新更新