Android Notification和NoSuchMethodError

时间:2014-03-06 14:41:11

标签: java android notifications compatibility nosuchmethoderror

我建立了一个通知:

Notification.Builder builder = new Notification.Builder(
                            getApplicationContext())
                            .setTicker(
                                    getApplicationContext().getString(
                                            R.string.my_string))
                            .setSmallIcon(android.R.drawable.sym)
                            .setContentTitle(
                                    getApplicationContext().getString(
                                            R.string.my_string_two))
                            .setContentText(a.getB())
                            .setSound(
                                    RingtoneManager
                                            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                            .setVibrate(new long[] { 10001000 })
                            .setAutoCancel(true)
                            .setContentIntent(PendingIntent.getActivity(getApplicationContext(),0,
                                                    new Intent()
                                                            .setAction(Intent.ACTION_VIEW)
                                                            .setType(CallLog.Calls.CONTENT_TYPE)
                                                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                                                    0));
                    NotificationManager nm = (NotificationManager) getApplicationContext()
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                    nm.notify("interstitial_tag", 1, builder.build());

使用android 4.0我发现了一个错误:NoSuchMethodError。 我怎么解决呢?我使用Notification.Compact吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

你可以尝试:

public static void sendNotification(Context context, String info){
        NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(context);
//title notifications
notifyBuilder.setContentTitle(context.getString(R.string.app_name));
//small icon
notifyBuilder.setSmallIcon(R.drawable.ic_launcher);
//set contentText
notifyBuilder.setContentText(info);
notifyBuilder.setVibrate(new long[]{100, 200, 100, 500});       notifyBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
//setAutoCancel
notifyBuilder.setAutoCancel(true);

getNotificationManager(context).notify(0, notifyBuilder.build());
}

public final static NotificationManager getNotificationManager(Context context) {
return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}

当您使用新的Intent()时,请插入目标类

Intent wrapperIntent = new Intent(context, SenderBroadcast.class);
wrapperIntent.putExtra("KEY_UID", uid);
wrapperIntent.setData(Uri.parse("senderbroadcast://"+uid));
wrapperIntent.setAction("REQUESTCODE_SENDERBROADCAST");
PendingIntent.getActivity(context, RequestCode.REQUESTCODE_SENDERBROADCAST, wrapperIntent, PendingIntent.FLAG_UPDATE_CURRENT);