Android无法使用广播通知程序启动intentservice

时间:2013-11-25 18:18:15

标签: android broadcast intentservice

我无法使用通知程序启动服务。 这是类MainActivity - 类启动服务:

package com.edu.serviceexam;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

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

        Intent intent = new Intent(this, NetworkService.class);
        intent.putExtra("doinbackground", "params");
        startService(intent);
    }
}

服务类,我用上下文创建一个广播,但它错误。

package com.edu.serviceexam;    
import android.app.IntentService;
import android.content.Intent;

public class NetworkService extends IntentService{

    // Defines and instantiates an object for handling status network.
    private BroadcastNotifier mBroadcaster = new BroadcastNotifier(this); // Error here

    public NetworkService() {
        super("NetworkService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // Do in background ...
        mBroadcaster.notifyProgress("notify message");

    }    
}

BroadcastNotifier:

package com.edu.serviceexam;    
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

public class BroadcastNotifier {

    private LocalBroadcastManager mBroadcaster;

    public BroadcastNotifier(Context context) {

        // Gets an instance of the support library local broadcastmanager
        mBroadcaster = LocalBroadcastManager.getInstance(context);

    }

    public void notifyProgress(String logData) {

        Intent localIntent = new Intent();

        // Setup localIntent here.

        // Broadcasts the Intent
        mBroadcaster.sendBroadcast(localIntent);

    }
}

这是我的清单

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.edu.serviceexam.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="com.edu.serviceexam.NetworkService"
            android:exported="false" />
    </application>

</manifest>

任何人都可以帮我解决这个问题吗?非常感谢!

0 个答案:

没有答案