Android:广播接收器错误

时间:2013-06-22 11:36:45

标签: android android-context android-broadcast

我有一个广播接收器。这是源代码......

package com.anadolu.SmsBroadcasts;

import com.anadolu.SmsActivity.ActivityMessages;
import com.anadolu.R;
import com.anadolu.SmsFramework.InformationFactory;
import com.anadolu.SmsFramework.InformationFactory.ContactIdentification;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class BroadcastMessageReceiver extends BroadcastReceiver{

private Context ActivityContext;
private Intent ActivityIntent;

private InformationFactory Factory;

private SharedPreferences ApplicationSettings;

private NotificationManager NotificationMan;
private NotificationCompat.Builder NotificationLay;

private Intent NotificationIntent;
private PendingIntent ContentIntent;

@Override
public void onReceive(Context ApplicationContext, Intent ApplicationIntent) {
    try{
        this.ActivityContext = ApplicationContext;
        this.ActivityIntent = ApplicationIntent;

        Factory = new InformationFactory(ActivityContext);

        ApplicationSettings = PreferenceManager.getDefaultSharedPreferences(ActivityContext);
        PreferenceManager.setDefaultValues(ActivityContext, R.xml.settings, false);

        Bundle SmsBundle = ActivityIntent.getExtras();
        SmsMessage[] Message = null;

        if(SmsBundle != null){
            Object[] pdus = (Object[]) SmsBundle.get("pdus");
            Message = new SmsMessage[pdus.length];

            for (int i=0; i < Message.length; i++){
                Message[i] = SmsMessage.createFromPdu((byte[])pdus[i]);

                if(ApplicationSettings.getBoolean("SettingsDisableNotifications", false)) {
                    this.abortBroadcast();
                    insertSmsMessage(Message[i].getOriginatingAddress(), Message[i].getTimestampMillis(), 0, -1, 1, Message[i].getMessageBody());
                }
                if(ApplicationSettings.getBoolean("SettingsShowNotifications", true))
                    showNotificationMessage(Message[i].getOriginatingAddress(), Message[i].getMessageBody());
            }
        }
    } catch (Exception Error) {
        Toast.makeText(ActivityContext, Error.toString(), Toast.LENGTH_LONG).show();
    }
}

private void insertSmsMessage(String Address, Long Date, int Read, int Status, int Type, String Body){
    ContentValues MessageValues = new ContentValues();

    MessageValues.put("address", Address);
    MessageValues.put("date", Date);
    MessageValues.put("read", Read);
    MessageValues.put("status", Status);
    MessageValues.put("type", Type);
    MessageValues.put("body", Body);

    ActivityContext.getContentResolver().insert(Uri.parse("content://sms"), MessageValues);
}

@SuppressWarnings("static-access")
private void showNotificationMessage(String Address, String Body){
    try {
        ContactIdentification Information = Factory.getContactIndentificationFromAddress(Address);

        String DisplayName = null;
        if(Information.ContactName != null)
            DisplayName = Information.ContactName;
        else
            DisplayName = Address;

        Bitmap ContactImage = null;
        if(Information.ContactImage != null)
            ContactImage = Information.ContactImage;
        else
            ContactImage = BitmapFactory.decodeResource(ActivityContext.getResources(), R.drawable.contact);

        Bundle Extras = new Bundle();
        Extras.putInt("ThreadID", Factory.getThreadIdFromAddress(Address));
        Extras.putString("Address", Address);

        NotificationIntent = new Intent(ActivityContext, ActivityMessages.class);
        NotificationIntent.putExtras(Extras);

        ContentIntent = PendingIntent.getActivity(ActivityContext, 0, NotificationIntent, 0);

        NotificationMan = (NotificationManager) ActivityContext.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationLay = new NotificationCompat.Builder(ActivityContext)
        .setAutoCancel(true)
        .setContentTitle(DisplayName)
        .setContentIntent(ContentIntent)
        .setContentText(Body)
        .setLargeIcon(ContactImage)
        .setSmallIcon(R.drawable.contact)
        .setTicker(DisplayName + " " + ActivityContext.getResources().getString(R.string.GeneralNotificationSummary))
        .setWhen(System.currentTimeMillis());

        NotificationLay.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        NotificationLay.setVibrate(new long[] {0, 400, 150, 150, 50, 100});
        NotificationLay.setLights(Color.BLUE, 750, 250);

        NotificationMan.notify(BroadcastValues.NOTIFICATION_ID, NotificationLay.build());
    } catch (Exception Error) {
        Log.e("----------Broadcast----------", Error.toString());
    }
}

}

我有一个错误:(这是logcat;

06-22 14:27:35.637: E/AndroidRuntime(10391): FATAL EXCEPTION: main
06-22 14:27:35.637: E/AndroidRuntime(10391): java.lang.NoClassDefFoundError:    android.support.v4.app.NotificationCompat$Builder
06-22 14:27:35.637: E/AndroidRuntime(10391):    at com.anadolu.SmsBroadcasts.BroadcastMessageReceiver.showNotificationMessage    (BroadcastMessageReceiver.java:117)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at com.anadolu.SmsBroadcasts.BroadcastMessageReceiver.onReceive(BroadcastMessageReceiver.java:69)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2139)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at android.app.ActivityThread.access$1500(ActivityThread.java:127)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at android.os.Looper.loop(Looper.java:137)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at android.app.ActivityThread.main(ActivityThread.java:4441)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at java.lang.reflect.Method.invokeNative(Native Method)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at java.lang.reflect.Method.invoke(Method.java:511)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
06-22 14:27:35.637: E/AndroidRuntime(10391):    at dalvik.system.NativeStart.main(Native Method)

我在onRerceive()方法中删除了showNotificationMessage()方法,这是在运行......可以做什么?

  

不是:ActivityThreads是我的主要课程......

2 个答案:

答案 0 :(得分:1)

libs目录中添加android支持库,而不是在构建路径中。

答案 1 :(得分:1)

试一试

打开Android项目的属性(甚至包括其他项目包含的属性),选择Java Build Path,Order and Export,最后检查Android Private Libraries。

Refrence