简体   繁体   中英

IntentService is not called from BroadCastReceiver

below codes are IntentService , BoradCastReceiver , MainAcitivity, Menifest

Actually When the app is started IntentService is called from MainActivity

When called from MainActivity, it works fine.

But When called from BroadCastReceiver, it seems not called.

Please tell me what i should do to solve it.

Menifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.location"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.location.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:name=".backgraound.LocationBackGround"
            android:exported="true" />

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyA_49-k8bdNVqMJkMTtl3hU97No3poJBzs" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <uses-library
            android:name="com.google.android.maps"
            android:required="true" />

        <receiver
            android:name=".backgraound.AlarmReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".backgraound.startBackGround"
            android:enabled="true" >
        </receiver>
    </application>

</manifest>

BroadCastReceiver

package com.example.location.backgraound;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class startBackGround extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast. makeText( context , "startBackGround", Toast.LENGTH_SHORT ).show();

        Intent mServiceIntent = new Intent(context, LocationBackGround.class);
        context.startService(mServiceIntent);
    }
}

IntentService

package com.example.location.backgraound;

import java.util.Iterator;
import java.util.List;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.example.location.db.MySQLiteHelper;
import com.example.location.meta.LocationNState;

public class LocationBackGround extends IntentService {


    private MySQLiteHelper db = null;
    private List<LocationNState> locationNStates = null;
    private Context context = null;

    private LocationManager mLocMan = null;
    private Location currentLocation = null;
    private AudioManager mAudioManager = null;



    public LocationBackGround() {
        super("LocationBackGround");
        // TODO Auto-generated constructor stub
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub

        context = getBaseContext();

        Toast.makeText( context , "intentService start : LocationBackGround", Toast.LENGTH_SHORT ).show();

        db = new MySQLiteHelper(context);
        locationNStates = db.getAllLocationNStates();

        mLocMan = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);


        requestLocation();

    }
}

When i boot up then i could see a Taost showing "StartBackGround".

But i cannot see "intentService start : LocationBackGround" Toast

is there any problem with my code?

Please teach me...

your code looks good. I use a very similar code with my service after booting the device. The difference in my project is that I call a Service - not an IntentService.

Maybe this will help you: http://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM