从服务中将数据发送到片段

时间:2014-06-23 19:19:40

标签: android broadcastreceiver android-service android-broadcast

我的服务中有一个字符串,我想将其发送到片段。但似乎我的BroadcastReceiver上的onReceive()方法从未调用过,我只是无法弄清楚原因。谢谢你的回答。

我的服务类

public class Servis extends Service {
static final String MAP = "MyService";
static final String USERID = "";

public static final String FILEPATH = "filepath";

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    System.out.println("starting service");
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
    String id = intent.getStringExtra("UserID");
    System.out.println(id);

    Intent intent1 = new Intent(MAP);
    intent1.putExtra(USERID, id);
    sendBroadcast(intent1);

    return START_REDELIVER_INTENT;
}

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

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(MAP, "onCreate");
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(MAP, "onDestroy");
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(MAP, "onStart");
}

}

我的片段类

package com.example.mobilproje;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapFragment extends FragmentActivity{

private GoogleMap googleHarita;

private BroadcastReceiver receiver = new BroadcastReceiver() {

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

        Bundle bundle = intent.getExtras();
        handleResult(bundle);
    }

};

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

    registerReceiver(receiver, new IntentFilter(
            Servis.MAP));

    if (googleHarita == null) {            
     googleHarita = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.haritafragment))  
                .getMap(); 
        if (googleHarita != null) 
            LatLng EvKoordinat = new LatLng(40.7994211,29.9517352);
            googleHarita.addMarker(new MarkerOptions().position(EvKoordinat).title("Ev"));
            googleHarita.moveCamera(CameraUpdateFactory.newLatLngZoom(EvKoordinat, 13));         
        }
    }

}

@Override
public void onResume() {        
    registerReceiver(receiver, new IntentFilter(
            Servis.MAP));
}

private void handleResult(Bundle bundle) {
    if (bundle != null) {
        String string = bundle.getString(Servis.USERID);
        System.out.println("string");
        }
    }
}

AndroidManifest

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

<uses-sdk
    android:minSdkVersion="13"
    android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />


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

<application
    android:allowBackup="true"
    android:icon="@drawable/nfc"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:theme="@style/mytheme"
        android:label="@string/app_name" >

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

        <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <action android:name="android.nfc.action.TAG_DISCOVERED" />
         </intent-filter>

        <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
    </activity>

    <activity
        android:name=".MapFragment"
        android:label="@string/app_name" >

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


    </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCgPPczPpQA_00_v-nTux_WuGuaO9egUpc" />

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

        <service android:enabled="true" android:name=".Servis"  android:process=":localservice"/>

</application>

            <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"/>
</manifest>

0 个答案:

没有答案
相关问题