【问题标题】:Start IntentService from other App从其他 App 启动 IntentService
【发布时间】:2015-05-25 00:35:58
【问题描述】:

我正在尝试从其他应用程序启动 IntentService,现在我有一个带有 AsyncTask 的 Activity,我可以在其中下载文件。这工作正常,但我想在 IntentService 中下载文件并且不必显示活动,只需一个通知。这可能吗?我已经读过 IntentService 在 Activity 通过 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
            }
        }
    }
}

这是 Activity 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 个错误

对不起我的英语????

【问题讨论】:

    标签: android-intentservice


    【解决方案1】:

    一个简单的解决方案是创建一个 BroadcastReceiver,在清单中注册它以执行一些意图操作,然后让它启动意图服务。

    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);
                }
            }
        }
    }
    

    在清单中:

    <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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多