2013-06-22 53 views
0

我有一个广播接收器。这是源代码Android:广播接收器错误

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) 

我删除了showNotificationMessage()方法onRerceive()方法,这是运行..什么我不能做

:ActivityThreads是我的主类...

回答

1

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

+0

这个问题是真实的,但不是libs目录,建立补丁:)非常感谢!... –

1

试试这个

打开你的Android项目(甚至由其他项目包括的那些),选择Java Build Path,订单和出口的性质,最后检查Android的私人图书馆。

Refrence

+0

这个问题是真实的,但不是Android的私人图书馆,安卓4.2.2图书馆:)非常感谢!.. 。 –