如何从后台服务显示状态通知或弹出对话框?

时间:2011-08-04 08:11:38

标签: android service

我创建了启动Service,它在我的应用程序中侦听传入的消息。服务在后台运行中持续运行思想应用程序关闭,即用户下线。当用户离线并且有新消息时,我必须在状态栏或弹出对话框中显示通知(此处此情况服务未绑定到任何活动)。我面临以下问题:

  1. 创建通知时,我没有获得上下文和活动。
  2. 当我从服务类显示对话框时,我得到例外"Can't create handler inside thread that has not called Looper.prepare()".
  3. 我是android服务部分的新人,不知道锄头来解决这个问题。 任何人都可以指导我如何做到这一点?有没有链接可以指导我这个?由于这个问题我被困了。高度赞赏。感谢。

4 个答案:

答案 0 :(得分:4)

您可以发送通知并指定用户启动时应运行的活动:

 private void sendNotification(Bundle bundle){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.icon;
    CharSequence tickerText = "bla bla";
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, ACTIVITY_YOU_WANT_TO_START.class);
    if(bundle!=null)
        notificationIntent.putExtras(bundle); //you may put bundle or not
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    int any_ID_you_want = 1; 
    //if you send another notification with same ID, this will be replaced by the other one
    mNotificationManager.notify(HELLO_ID, notification);
}

//To play a sound add this:
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); //for example

您可以开始另一项活动:

private boolean startActivity(Bundle bundle){
    Intent myIntent = new Intent(mContext, ACTIVITY_YOU_WANT_TO_START.class);
    if(bundle!=null)
         myIntent.putExtras(bundle);//optional
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getApplication().startActivity(myIntent);
    return true;
}

答案 1 :(得分:2)

除了警长的答案,你必须使用以下主题

android:theme="@android:style/Theme.Dialog"

用于AndroidManifest.xml中的该Activity。然后你会得到一个Dialog。 :)

答案 2 :(得分:1)

只有在系统警报对话框中,我们才能显示来自服务的对话框。因此,将TYPE_SYSTEM_ALERT窗口布局参数设置为Dialog,如下所示,

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

但是,它需要SYSTEM_ALERT_WINDOW permission。因此,不要忘记在Manifest文件中添加此权限。

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

快乐的编码......:)

答案 3 :(得分:1)

要从服务添加警报对话框,这可以正常工作

final AlertDialog dialog = dialogBuilder.create();
            final Window dialogWindow = dialog.getWindow();
            final WindowManager.LayoutParams dialogWindowAttributes = dialogWindow.getAttributes();

            // Set fixed width (280dp) and WRAP_CONTENT height
            final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialogWindowAttributes);
            lp.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 280, getResources().getDisplayMetrics());
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            dialogWindow.setAttributes(lp);

            // Set to TYPE_SYSTEM_ALERT so that the Service can display it
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                dialogWindow.setType(WindowManager.LayoutParams.TYPE_TOAST);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            }
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
            {
                dialogWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            }
            dialog.show();

但使用TYPE_SYSTEM_ALERT可能会触发使用危险权限的应用的Google删除政策。如果谷歌要求,请确保您有正确的理由。