后台服务没有开始

时间:2015-07-20 19:52:45

标签: android broadcastreceiver

大家好我认为我的广播没有收到BOOT_COMPLETEDSCREEN ON HEADSET_PLUG

这是我的代码:

清单

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/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>

        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.intent.action.HEADSET_PLUG" />
            </intent-filter>
        </receiver>
        <service android:name=".Service"/>

    </application>

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

</manifest>

Service.java

public class Service extends android.app.Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(getApplicationContext(), "Servicio iniciado", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getApplicationContext(), "Servicio terminado", Toast.LENGTH_LONG).show();
    }
}

MyReceiver

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent pushIntent = new Intent(context, Service.class);
            context.startService(pushIntent);
        }
        Log.i("received","yes");
    }
}

0 个答案:

没有答案