显示通知 ();方法不起作用,使应用程序崩溃

时间:2015-03-14 17:34:23

标签: android bluetooth

这是显示通知的方法我不能让它运行所以基本上它会显示蓝牙已断开连接的简单通知。此方法位于活动文件中。

代码中没有错误问题是我认为显示失效因为某些原因它在被调用时崩溃

public static void showNotification(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(SettingsActivity, NotificationHandler.class);
    PendingIntent pIntent = PendingIntent.getActivity(SettingsActivity, 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(SettingsActivity)

        .setContentTitle("New Post!")
        .setContentText("Here's an awesome update for you!")
        .setSmallIcon(R.drawable.logo)
        .setContentIntent(pIntent)
        .setSound(soundUri)

        .addAction(R.drawable.logo, "View", pIntent)
        .addAction(0, "Remind", pIntent)

        .build();

    NotificationManager notificationManager = (NotificationManager) c.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);

}

这就是我所说的方法shownotification(); on onTTDisconnect();它会显示通知。

public void onReceive(Context context, Intent intent)
{

    String action = intent.getAction();
    SettingsActivity SA = new SettingsActivity();

    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {

        isConnected = true;
        if(SettingsActivity.isRunning)
            SettingsActivity.onBTConnect();
    }
    else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { 

        Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_SHORT).show();
        isConnected = false; 

        if(SettingsActivity.isRunning)
            SettingsActivity.onBTDisconnect();
        SettingsActivity.showNotification(context);


    }else{

        //Handling of text messages received
        Bundle bundle= intent.getExtras();

        Object[] messages=(Object[])bundle.get("pdus");
        SmsMessage[] sms=new SmsMessage[messages.length];

        for(int n=0;n<messages.length;n++){
            sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
        }

        for(SmsMessage msg:sms){
            if(isValidNumber(context,msg.getOriginatingAddress()))
                TextMessageManager.getInstance().addNewMessage(context, msg.getOriginatingAddress(),msg.getMessageBody());
        }
    }

}

1 个答案:

答案 0 :(得分:0)

全局定义notificationManager(在扩展Activity ..之后)NotificationManager notificationManager;