2015-05-25 246 views
0

我試圖從其他應用程序啓動IntentService,現在我有一個活動與一個AsyncTask我下載文件。這工作正常,但我想下載IntentService中的文件,而不必顯示活動,只是一個通知。這可能嗎?我已閱讀,IntentService開始時一個活動通過startService(意向)調用它,但是當我嘗試沒有任何反應:(從其他應用程序啓動IntentService

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" /> 

My Class 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 AsynTas ķ。

最後清單:

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

對不起我的英語

回答

1

一個簡單的解決方案是創建一個BroadcastReceiver,將它註冊到您的清單中進行一些intent操作,然後讓它啓動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); 
      } 
     } 
    } 
} 

,並在清單:

<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); 
相關問題