MainActivity已经使用了IntentReceiver?

时间:2013-09-18 14:40:31

标签: java android broadcastreceiver android-manifest android-service

我在logcat中有这个错误: Activity com.mypackage.app.MainActivity has laked IntentReceiver com.mypackage.app.MainActivity$100000000@4202adb8 that was originally registred here. Are you missing a call to unregisterReceiver()?

该服务的代码是:

package com.mypackage.app;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.app.PendingIntent;
import android.app.Service;

public static class UpdateService extends Service {    

public final static String TAG = "Bat";

    //private static final String TAG = AndroidBatteryWidgetProvider.class.getSimpleName();
    public static Boolean debug     = true; 
        BatteryInfo mBI = null;     
        public void updateWidget(Context context, Intent batteryIntent){

            if (debug) Log.i(TAG,"---------- updateWidget");

            SimpleDateFormat formatter = new SimpleDateFormat(" HH:mm:ss ");

            RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.androidbatterywidget_layout);

            updateViews.setTextViewText(R.id.level, "waiting!");

            final int status = batteryIntent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);

            final int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

            final int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

            updateViews.setTextViewText(R.id.level, "" + level + " %" );

            updateViews.setTextViewText(R.id.time, formatter.format(new Date()));

            final int temperature = batteryIntent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);

            String tempString= String.format("%.0f°C", new Float(temperature/10));

            if (debug) Log.d(TAG,"BAT:" + tempString + " " + level + "%");

            updateViews.setTextViewText(R.id.temperature, tempString );

            final int voltage = batteryIntent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);

            updateViews.setTextViewText(R.id.voltage, "" + voltage + " mV" );


            updateViews.setOnClickPendingIntent(R.id.layout ,

                    PendingIntent.getActivity(context, 0, 

                            new Intent(context, AndroidBatteryActivity.class),Intent.FLAG_ACTIVITY_NEW_TASK));


            ComponentName myComponentName = new ComponentName(context, AndroidBatteryWidgetProvider.class);

            AppWidgetManager manager = AppWidgetManager.getInstance(context);

            manager.updateAppWidget(myComponentName, updateViews);


            //Second, update database in a thread       

            new Thread (new Runnable(){

                public void run(){

                    final Context c=getApplicationContext();

                    DBHelper db = new DBHelper(c);

                    db.record( level, status, plugged );

                    db.deleteOldEntries();

                    db.close();

                    if (debug) Log.i( TAG, "---------- Add record: " + level + " time: "+ Calendar.getInstance().getTimeInMillis() );   

                }
            }).start();


        }

        public void  handleCommand(Intent intent){

            if(mBI == null)

            {
                mBI = new BatteryInfo(this);

//              IntentFilter mIntentFilter = new IntentFilter();

//              mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
//              registerReceiver(mBI, mIntentFilter);

                // After registering mBI, another update is immediately processed.

                // So, skip double update processing.
                return;

            } 
            //update widget views and database

            updateWidget(getApplicationContext(), intent);


        }


        @Override
        public void onStart(Intent intent, int startId) {

            handleCommand(intent);            

        }


        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            if (debug)

                Log.d(TAG, "----------------- onStartCommand");     

            handleCommand(intent);     

            // The service has to be running otherwise the broadcast ACTION_BATTERY_CHANGED wont be received anymore

            // thats why it returns START_STICKY

            return START_STICKY;
        }

        @Override
        public void onDestroy() {

            super.onDestroy();

            try{                
                if(mBI != null) {

                    if (debug)

                    Log.d(TAG, "----------------- onDestroy: unregisterReceiver(mBI)" );
                    unregisterReceiver(mBI);

                }

            }catch(Exception e)

            {Log.e(TAG, "", e);}

        }
        @Override

        public IBinder onBind(Intent arg0) {

            // TODO Auto-generated method stub
            return null;

        }
    }   

}

我找不到错误..我也发布了清单:

<service android:name=".UpdateService" /> 
        <receiver android:name=".BatteryInfo" android:label="BatteryInfo">
        <intent-filter>
            <action android:name="android.intent.action.BATTERY_CHANGED" /> 
        </intent-filter>
        </receiver>

        <activity
            android:name=".AndroidBatteryActivity"
            android:label="@string/app_name" >
        </activity>
        <activity android:name="org.achartengine.GraphicalActivity" >
        </activity>

任何帮助?

0 个答案:

没有答案