Abdulkadir NURKALEM
Abdulkadir NURKALEM

Reputation: 749

Android: Broadcast Receiver Error

I have a broadcast receiver. This is source code...

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());
    }
}

}

And I have a error :( This is 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)

I deleted the showNotificationMessage() method in onRerceive() method, this is running... What can ı do ?

Not: ActivityThreads is my main class...

Upvotes: 0

Views: 669

Answers (2)

CRUSADER
CRUSADER

Reputation: 5472

Give this a try

Open the properties of your Android projects (even the ones that are included by other projects), select Java Build Path, Order and Export, and finally check Android Private Libraries.

Refrence

Upvotes: 1

Ali
Ali

Reputation: 2042

Add android support library in libs directory, not in the build path.

Upvotes: 1

Related Questions