2016-05-27 8 views
0

Ich habe eine MainActivity, die nichts und eine Klasse tun BootCompletedIntentReceiver genannt, die extends BroadcastReciever und eine weitere Klasse mit dem Namen MyServices die extends Service. Alles, was ich tun möchte, ist Autorun meiner App nach dem Einschalten des Geräts, die von BootCompletedIntentReceiver behandelt wird und danach startet eine Service, die einen Toast 8 Mal zeigt. Aber wenn ich mein Gerät neu starte passiert nichts. Ich habe mein Bestes versucht, dies zu lösen. Kann mir jemand zu finden, in denen iProbleme mit BroadcastReceiver und Service

Hier ist meine BootCompletedIntentReceiver Code stecke immer:

package com.anshuman.myapplication; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class BootCompletedIntentReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
     Intent pushIntent = new Intent(context, MyService.class); 
     context.startService(pushIntent); 
    } 
} 
} 

Und hier ist MyService Klasse:

package com.anshuman.myapplication; 

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

public class MyService extends Service { 

@Override 
public void onCreate() { 
    super.onCreate(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId){ 
    int a=2; 
    while (a<10) { 
     Toast.makeText(this, "onStartCommand", Toast.LENGTH_LONG).show(); 
     a++; 
    } 

    return Service.START_STICKY; 
} 


@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 
} 

Und meine AndroidManifest Datei:

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

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <receiver android:name=".BootCompletedIntentReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service android:name=".MyService"/> 
</application> 

+0

print logs in service und broadcast und toast entfernen. Danach überprüfen Sie Ihre App im Gerät –

+0

Sie müssen die App mindestens einmal manuell nach der Installation starten.Andernfalls wird die BOOT_COMPLETE nicht zu Ihrem "BroadcastReceiver" geliefert. –

+0

Auch Ihre Terminologie ist verwirrend. Ein 'BroadcastReceiver' ist keine' Activity' und ein 'Service' ist auch keine' Activity'. Ich habe deinen Beitrag entsprechend bearbeitet. –

Antwort

0

Code funktioniert gut Problem ist, dass nur mein Xiaomi-Gerät mit diesem Code nicht funktioniert.

Verwandte Themen