2017-01-10 1 views
0

Ich bin neu in Android-Stack. Ich versuche, Android Service von der launcher activity zu starten. Service und Activity sind in separaten Paketen definiert, werden aber nicht gestartet. In der logcat gibt es keine Ausnahme oder Fehler. Ich habe viele Fragen zu stackoverflow in Bezug auf dieses Problem überprüft, aber das hat nicht funktioniert. Im Folgenden finden Sie den Quellcode meiner App. Ich habe fast 8 Stunden mit diesem Thema verbracht. Jede Hilfe wäre eine große Wertschätzung.nicht in der Lage, Android-Dienst von der Aktivität zu starten, die in dem anderen Paket ist

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

    <supports-screens 
     android:anyDensity="true" 
     android:largeScreens="true" 
     android:normalScreens="true" 
     android:resizeable="true" 
     android:smallScreens="true" 
     android:xlargeScreens="true" /> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme">   
     <activity 
      android:name=".ui.LoginActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity>  
     <service 
      android:name=".messaging.AlertService" 
      android:enabled="true" 
      android:exported="true"> 
     </service> 
    </application> 
</manifest> 

AlertService.java:

package nl.test.app.messaging; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class AlertService extends Service { 

    public AlertService() { 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Toast.makeText(getApplicationContext(), "on create called\n", Toast.LENGTH_LONG).show(); 

    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return START_STICKY; 
    } 
} 

LoginActivity.java

package nl.test.app.ui; 

import android.content.ComponentName; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.EditText; 
import android.widget.Toast; 

import nl.test.app.R; 

public class LoginActivity extends AppCompatActivity{ 

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

    } 

    // function for service testing 
    public void onStartButtonClick(View view) { 
     Intent myIntentToStartAlertListActivity = new Intent(); 
     String pkg = "nl.test.app.messaging"; 
     String cls = "nl.test.app.messaging.AlertService"; 
     myIntentToStartAlertListActivity.setComponent(new ComponentName(pkg, cls)); 

    if (startService(myIntentToStartAlertListActivity) != null) { 
      Log.i("Service Started","Service started"); 
      Toast.makeText(getApplicationContext(), "Service is running\n", Toast.LENGTH_LONG).show(); 
     } 
     else { 
      Toast.makeText(getApplicationContext(), "Service is not running\n", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 

    } 
} 
+0

gibt es zwei Definitionen von 'h Versandstücke ehe. Meinst du sie sind in verschiedenen Java-Namespaces oder in verschiedenen Android-Apps? –

Antwort

1
// function for service testing 
public void onStartButtonClick(View view) { 
    Intent myIntentToStartAlertListActivity = new Intent(LoginActivity.this,AlertService.class); 
    String pkg = "nl.test.app.messaging"; 
    String cls = "nl.test.app.messaging.AlertService"; 
    myIntentToStartAlertListActivity.setComponent(new ComponentName(pkg, cls)); 
    //startService(myIntentToStartAlertListActivity) 

if (startService(myIntentToStartAlertListActivity) != null) { 
     Log.i("Service Started","Service started"); 
     Toast.makeText(getApplicationContext(), "Service is running\n", Toast.LENGTH_LONG).show(); 
    } 
    else { 
     Toast.makeText(getApplicationContext(), "Service is not running\n", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

danke es hat funktioniert! –

1

Versuchen Sie, diese

public void onStartButtonClick(View view) { 
    Intent myIntentToStartAlertListActivity = new Intent(LoginActivity.this, AlertService.class); 
if (startService(myIntentToStartAlertListActivity) != null) { 
     Log.i("Service Started","Service started"); 
     Toast.makeText(getApplicationContext(), "Service is running\n", Toast.LENGTH_LONG).show(); 
    } 
    else { 
     Toast.makeText(getApplicationContext(), "Service is not running\n", Toast.LENGTH_LONG).show(); 
    } 
} 
Verwandte Themen