NotificationCompat.Builder不起作用,安卓2.2.1 [英] NotificationCompat.Builder doesn't work, Android 2.2.1

查看:132
本文介绍了NotificationCompat.Builder不起作用,安卓2.2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是code:

  NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(
        本).setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(我的通知)
        .setContentText(的Hello World!);
 

为什么它不工作?它显示什么。测试在Android 2.2.1。

更新。 $ C $活动C:

 进口android.app.Activity;
进口android.app.AlertDialog;
进口android.os.Bundle;
进口android.support.v4.app.NotificationCompat;
进口android.view.Menu;
进口android.view.View;

公共类LoginActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_login);
    }

    公共无效启动(查看视图){
        NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(
                本).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(我的通知)
                .setContentText(的Hello World!);
    }
}
 

按钮布局:

 <按钮
    机器人:ID =@ + ID / startButton
    机器人:layout_width =100dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID / aboutButton
    机器人:layout_alignLeft =@ + ID / aboutButton
    机器人:layout_marginBottom =20dp
    机器人:的onClick =开始
    机器人:文本=开始/>
 

UPDATE2。错误日志:

  11月2日至4号:40:40.752:D / dalvikvm(23054):GC_EXTERNAL_ALLOC释放859对象/ 59168字节61ms
11月2号至4号:40:56.424:D / AndroidRuntime(23054):关闭虚拟机
11月2号至4号:40:56.424:W / dalvikvm(23054):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d888)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):致命异常:主要
11月2号至4号:40:56.432:E / AndroidRuntime(23054):java.lang.IllegalStateException:无法执行活动的方法
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.view.View $ 1.onClick(View.java:2082)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.view.View.performClick(View.java:2461)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.view.View $ PerformClick.run(View.java:8890)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.os.Handler.handleCallback(Handler.java:587)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.os.Handler.dispatchMessage(Handler.java:92)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.os.Looper.loop(Looper.java:123)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.app.ActivityThread.main(ActivityThread.java:4632)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在java.lang.reflect.Method.invokeNative(本机方法)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在java.lang.reflect.Method.invoke(Method.java:521)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在dalvik.system.NativeStart.main(本机方法)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):由:java.lang.reflect.InvocationTargetException
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在pckg.mywebsites.LoginActivity.start(LoginActivity.java:28)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在java.lang.reflect.Method.invokeNative(本机方法)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在java.lang.reflect.Method.invoke(Method.java:521)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.view.View $ 1.onClick(View.java:2077)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):11 ...更多
11月2号至4号:40:56.432:E / AndroidRuntime(23054):java.lang.IllegalArgumentException异常:产生的原因contentIntent要求:PKG = pckg.mywebsites ID = 222 =通知的通知(振动= NULL,声音= null,则默认=为0x0)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.os.Parcel.readException(Parcel.java:1264)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.os.Parcel.readException(Parcel.java:1248)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.app.NotificationManager.notify(NotificationManager.java:110)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):在android.app.NotificationManager.notify(NotificationManager.java:90)
11月2号至4号:40:56.432:E / AndroidRuntime(23054):15 ...更多
 

解决方案

它的工作原理:

 意图resultIntent =新的意图(这一点,LoginActivity.class);
PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    本,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);

NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(
        getApplicationContext())。setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(我的通知)
        .setContentText(的Hello World!)
        .setContentIntent(resultPendingIntent);

NotificationManager经理=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID,mBuilder.build());
 

I have next code:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        this).setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("My notification")
        .setContentText("Hello World!");

Why it doesn't work? It shows nothing. Testing on Android 2.2.1.

Update. Code of activity:

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;

public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    public void start(View view) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!");
    }
}

Button in layout:

<Button
    android:id="@+id/startButton"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/aboutButton"
    android:layout_alignLeft="@+id/aboutButton"
    android:layout_marginBottom="20dp"
    android:onClick="start"
    android:text="Start" />

UPDATE2. Error log:

02-04 11:40:40.752: D/dalvikvm(23054): GC_EXTERNAL_ALLOC freed 859 objects / 59168 bytes in 61ms
02-04 11:40:56.424: D/AndroidRuntime(23054): Shutting down VM
02-04 11:40:56.424: W/dalvikvm(23054): threadid=1: thread exiting with uncaught exception (group=0x4001d888)
02-04 11:40:56.432: E/AndroidRuntime(23054): FATAL EXCEPTION: main
02-04 11:40:56.432: E/AndroidRuntime(23054): java.lang.IllegalStateException: Could not execute method of the activity
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View$1.onClick(View.java:2082)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View.performClick(View.java:2461)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.view.View$PerformClick.run(View.java:8890)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Handler.handleCallback(Handler.java:587)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Looper.loop(Looper.java:123)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.app.ActivityThread.main(ActivityThread.java:4632)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at dalvik.system.NativeStart.main(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054): Caused by: java.lang.reflect.InvocationTargetException
02-04 11:40:56.432: E/AndroidRuntime(23054):    at pckg.mywebsites.LoginActivity.start(LoginActivity.java:28)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.view.View$1.onClick(View.java:2077)
02-04 11:40:56.432: E/AndroidRuntime(23054):    ... 11 more
02-04 11:40:56.432: E/AndroidRuntime(23054): Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=pckg.mywebsites id=222 notification=Notification(vibrate=null,sound=null,defaults=0x0)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Parcel.readException(Parcel.java:1264)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Parcel.readException(Parcel.java:1248)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.app.NotificationManager.notify(NotificationManager.java:110)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.app.NotificationManager.notify(NotificationManager.java:90)
02-04 11:40:56.432: E/AndroidRuntime(23054):    ... 15 more

解决方案

It works:

Intent resultIntent = new Intent(this, LoginActivity.class);
PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setContentIntent(resultPendingIntent);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, mBuilder.build());

这篇关于NotificationCompat.Builder不起作用,安卓2.2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆