2015-02-25 17 views
6

W mojej aplikacji próbuję wysłać dane z mojego MainActivity.class do usługi o nazwie bgservice.class. To jest mój poniższy kod:Android- Jak przesłać dane z działania do serwisu?

MainActivity.class:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent serviceIntent = new Intent(bgservice.class.getName()); 
    serviceIntent.putExtra("UserID", "123456"); 
    this.startService(serviceIntent); 
} 

bgservice.class:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    String userID = intent.getStringExtra("userID"); 
    System.out.println(userID); 
    Toast.makeText(this,userID, Toast.LENGTH_LONG).show(); 
    return START_STICKY; 

} 

ale nie otrzymuję dane w klasy usług. Są to następujące błąd pojawia się:

02-25 09: 05: 41,166: E/Android Runtime (2633):

java.lang.RuntimeException: Nie można rozpocząć działalność
ComponentInfo {KOM. microapple.googleplace/com.microapple.googleplace. główną działalność}: java.lang.IllegalArgumentException: obsługa zamiar musi być jawne : Intent {act = com.microapple.googleplace.bgservice (ma dodatki)}

AndroidManifest.xml:

 ... 
    <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    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=".bgservice" /> 
</application> 
.... 
+0

czy dodałeś tę usługę do swojego manifestu? –

+0

ya dodałem @RandykaYudhistira –

+0

@ Balau1: Czy dodałeś akcję com.microapple.googleplace.bgservice' do manifestu? –

Odpowiedz

3

tutaj:

Intent serviceIntent = new Intent(bgservice.class.getName()); 

Przechodząc String do Intent konstruktor do tworzenia zamiar uruchomić usługę. Intent constructor bierze String jako nazwę Action, którą dodaliśmy w AndroidManifest.xml.

<service android:enabled="true" android:name=".bgservice"> 
<intent-filter > 
     <action android:name="com.microapple.googleplace.bgservice" /> 
    </intent-filter> 
</service> 

Teraz użyj com.microapple.googleplace.bgservice jak nazwa działania, aby utworzyć zamiar:

 Intent serviceIntent = new Intent("com.microapple.googleplace.bgservice"); 
     serviceIntent.putExtra("UserID", "123456"); 
     this.startService(serviceIntent); 

LUB

Zastosowanie Intent dusiciel która odbywa kontekście jako pierwszy parametr i nazwę komponentu, który chcemy uruchomić jako drugi parametr:

Intent serviceIntent = new Intent(this,bgservice.class); 
    serviceIntent.putExtra("UserID", "123456"); 
    this.startService(serviceIntent); 

A także używać tego samego klucza, który korzystając dodać dane intencyjny obecnie dodawanie wartości z UserID klucza, ale stara się uzyskać wartość używając userID klucz

+0

@prosper k Próbowałem tego, ale otrzymałem ten sam błąd –

+0

@ Balau1: Którą próbą próbowałeś?proszę pokazać zaktualizowany kod –

+0

@ Balau1: wypróbuj za pomocą 'Intent serviceIntent = new Intent (this, bgservice.class);' –

2
Intent serviceIntent = new Intent(YourActivity.this, bgservice.class); 
     serviceIntent.putExtra("UserID", "123456"); 
     this.startService(serviceIntent); 

A w swojej służbie,

public int onStartCommand(Intent intent, int flags, int startId) { 

    String userID = intent.getStringExtra("UserID"); 

    //do something 

    return START_STICKY; 

} 

To musi pracować.

+0

Co mogę zrobić, jeśli chcę używać zmiennej userID w metodzie onCreate() usługi? Przepraszamy za komentowanie odpowiedzi od 2015. – PeMaCN

+0

Możesz pobrać tę zmienną w onCreate() jako, 'Intent intent = getIntent();', 'if (intent! = Null && intent.hasExtra (" userID ")) {String userID = intent.getExtra ("identyfikator_użytkownika")} ' – Apurva

+0

getIntent() nie działa w usługach w jaki sposób mogę wysłać dane paczkowane to – SAVVY

Powiązane problemy