向通知栏发送通知

时间:2013-12-03 17:34:05

标签: java android notifications

我试图制作一个其他类可以重用的Notification Activity,并提供额外的信息。我正在努力的是如何正确地从另一个类调用该方法。

通知活动代码:

公共类NotificationTest扩展了MyActivity {

public void showNotification(String s, Context c) {


    // define sound URI, the sound to be played when there's a notification

    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    // intent triggered, you can add other intent for other actions

    //Intent intent = new Intent(Sender context, which class to start);

    Intent intent = new Intent(c, Settings.class);

    //PendingIntent pIntent = PendingIntent.getActivity(sender context, 0, intent, 0);
    PendingIntent pIntent = PendingIntent.getActivity(c, 0, intent, 0);


    // this is it, we'll build the notification!

    // in the addAction method, if you don't want any icon, just set the first param to 0

    Notification mNotification = new Notification.Builder(c)


            .setContentTitle("New Post!")


            .setContentText(s)

            .setSmallIcon(R.drawable.icon)

            .setContentIntent(pIntent)

            .setSound(soundUri)

                    //.addAction(R.drawable.icon, "View",pIntent)

                    //.addAction(0, "Remind", pIntent)

            .build();

    Log.d("Vindsiden", "Notification");

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


    // If you want to hide the notification after it was selected, do the code below

    // myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);


}


   }

从MyActivity调用通知的代码:

String s= "Notification";
    NotificationTest notificationTest = new NotificationTest();

    notificationTest.showNotification(s,MyActivity.this);

我得到的错误是:

12-05 19:20:34.004  22352-22352/com.vindsiden.windwidget E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.vindsiden.windwidget, PID: 22352
java.lang.IllegalStateException: System services not available to Activities before onCreate()
        at android.app.Activity.getSystemService(Activity.java:4531)
        at com.vindsiden.windwidget.NotificationTest.showNotification(NotificationTest.java:66)
        at com.vindsiden.windwidget.MyActivity$1.onClick(MyActivity.java:52)
        at android.view.View.performClick(View.java:4424)
        at android.view.View$PerformClick.run(View.java:18383)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4998)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

2 个答案:

答案 0 :(得分:2)

希望这有帮助...我用它。创建一个名为OurNotification的类

将所有Menu.class更改为您想要点击的位置。

public class OurNotification {


public void createNotification(Context context, String title, String msg, String other) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.notification)
        .setContentTitle(title)
        .setContentText(msg);

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, Menu.class);

// add so it removes
mBuilder.setAutoCancel(true);

//add vibrate and sound only use if you have the correct permissions in manifest or it will crash
//  long[] vibrate = {0,100,200,300};
//    mBuilder.setVibrate(vibrate);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));      
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
mBuilder.setOnlyAlertOnce(true);

if (other != null) {
    NotificationCompat.BigTextStyle bigStyle =
        new NotificationCompat.BigTextStyle();
    // Sets a title for the Inbox style big view
    bigStyle.setBigContentTitle(other);
    // Moves the big view style object into the notification object.
    mBuilder.setStyle(bigStyle);
}

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(Menu.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(200, mBuilder.build());
}
}

然后在活动服务或任何地方打电话

new   OurNotification notificaionToShow = new OurNotification();

notificaionToShow.createNotification(getApplicationContext(),“TITLE”,“Message”,null);

如果你想使用最后一个null的大样式更改到更多文本。

答案 1 :(得分:0)

很难确定,因为我认为您更改了示例的类名,但将原始文件保留在堆栈跟踪中。但是,这似乎是

java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4531)

是由此引起的:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

因此,我相信您传递给showNotification方法的Context是尚未创建的Activity。

在调用此方法的代码中,尝试使用getApplicationContext()getBaseContext()替换您传递给Context的任何内容。