2

所以这是一个简单的应用程序,它从用户那里获取两个文本输入,然后在按钮上单击它会创建通知。这是代码:

package com.dewey.notifymanager;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button) findViewById(R.id.button1);
        final TextView texty = (TextView) findViewById(R.id.textView2);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final EditText input2 = (EditText) findViewById(R.id.editText2);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String notificationmessage = input.getText().toString();
                String notificationdetails = input2.getText().toString();
                texty.setText("Notification Created");
                displayNotification(notificationmessage, notificationdetails);
            }
        });
    }
    @Override
    protected void onStart() {

    super.onStart();


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



    public void displayNotification(String msg,String details)
    {
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, msg, System.currentTimeMillis());




    notification.setLatestEventInfo(this, msg, details, null);
    int num = 0;
    manager.notify(num, notification);

    }

}

该应用程序在模拟器上运行并显示通知: 模拟器

但是当我在手机上运行它时,点击按钮时不会显示通知,当我在运行 Android 2.3 的手机上运行它时,应用程序崩溃并在点击按钮时意外停止。

这是日志猫:

01-09 13:21:29.653: D/AndroidRuntime(981): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
01-09 13:21:29.653: D/AndroidRuntime(981): CheckJNI is ON
01-09 13:21:30.773: D/AndroidRuntime(981): Calling main entry com.android.commands.pm.Pm
01-09 13:21:30.832: D/AndroidRuntime(981): Shutting down VM
01-09 13:21:30.842: D/dalvikvm(981): GC_CONCURRENT freed 101K, 78% free 462K/2048K, paused 1ms+2ms
01-09 13:21:30.853: D/dalvikvm(981): Debugger has detached; object registry had 1 entries
01-09 13:21:30.872: I/AndroidRuntime(981): NOTE: attach of thread 'Binder Thread #3' failed
01-09 13:21:31.863: D/AndroidRuntime(994): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
01-09 13:21:31.863: D/AndroidRuntime(994): CheckJNI is ON
01-09 13:21:32.912: D/AndroidRuntime(994): Calling main entry com.android.commands.am.Am
01-09 13:21:32.952: I/ActivityManager(85): Force stopping package com.dewey.notifymanager uid=10040
01-09 13:21:32.952: I/Process(85): Sending signal. PID: 965 SIG: 9
01-09 13:21:32.973: I/ActivityManager(85):   Force finishing activity ActivityRecord{415d64e0 com.dewey.notifymanager/.MainActivity}
01-09 13:21:33.002: W/InputDispatcher(85): channel '4160dde0 com.dewey.notifymanager/com.dewey.notifymanager.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
01-09 13:21:33.002: E/InputDispatcher(85): channel '4160dde0 com.dewey.notifymanager/com.dewey.notifymanager.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-09 13:21:33.002: I/WindowManager(85): WIN DEATH: Window{4160dde0 com.dewey.notifymanager/com.dewey.notifymanager.MainActivity paused=true}
01-09 13:21:33.002: W/InputDispatcher(85): Attempted to unregister already unregistered input channel '4160dde0 com.dewey.notifymanager/com.dewey.notifymanager.MainActivity (server)'
01-09 13:21:33.092: I/WindowManager(85): WINDOW DIED Window{4160dde0 com.dewey.notifymanager/com.dewey.notifymanager.MainActivity paused=true}
01-09 13:21:33.092: I/ActivityManager(85): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.dewey.notifymanager/.MainActivity} from pid 994
01-09 13:21:33.103: W/WindowManager(85): Failure taking screenshot for (180x300) to layer 21045
01-09 13:21:33.193: D/AndroidRuntime(994): Shutting down VM
01-09 13:21:33.223: D/dalvikvm(1004): Not late-enabling CheckJNI (already on)
01-09 13:21:33.223: I/AndroidRuntime(994): NOTE: attach of thread 'Binder Thread #3' failed
01-09 13:21:33.223: D/dalvikvm(994): GC_CONCURRENT freed 102K, 77% free 485K/2048K, paused 1ms+1ms
01-09 13:21:33.223: D/jdwp(994): Got wake-up signal, bailing out of select
01-09 13:21:33.233: D/dalvikvm(994): Debugger has detached; object registry had 1 entries
01-09 13:21:33.263: I/ActivityManager(85): Start proc com.dewey.notifymanager for activity com.dewey.notifymanager/.MainActivity: pid=1004 uid=10040 gids={}
01-09 13:21:33.733: W/NetworkManagementSocketTagger(85): setKernelCountSet(10040, 1) failed with errno -2
01-09 13:21:34.023: W/ActivityThread(1004): Application com.dewey.notifymanager is waiting for the debugger on port 8100...
01-09 13:21:34.042: I/System.out(1004): Sending WAIT chunk
01-09 13:21:34.064: I/dalvikvm(1004): Debugger is active
01-09 13:21:34.242: I/System.out(1004): Debugger has connected
01-09 13:21:34.242: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:34.553: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:34.752: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:34.962: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:35.162: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:35.362: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:35.563: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:35.782: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:36.024: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:36.281: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:36.525: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:36.734: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:36.940: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:37.148: I/System.out(1004): waiting for debugger to settle...
01-09 13:21:37.357: I/System.out(1004): debugger has settled (1477)
01-09 13:21:39.322: D/dalvikvm(85): GREF has increased to 501
01-09 13:21:39.473: D/gralloc_goldfish(1004): Emulator without GPU emulation detected.
01-09 13:21:39.622: I/ActivityManager(85): Displayed com.dewey.notifymanager/.MainActivity: +6s463ms
01-09 13:21:40.053: W/NetworkManagementSocketTagger(85): setKernelCountSet(10004, 0) failed with errno -2
01-09 13:21:42.673: D/dalvikvm(85): GC_CONCURRENT freed 488K, 7% free 12839K/13767K, paused 8ms+50ms
01-09 13:21:42.862: D/dalvikvm(139): GC_CONCURRENT freed 408K, 34% free 11007K/16519K, paused 5ms+10ms
01-09 13:21:46.232: D/dalvikvm(139): GC_FOR_ALLOC freed 400K, 34% free 10990K/16519K, paused 316ms
01-09 13:21:51.422: D/PhoneStatusBar(139): panel: ACTION_DOWN at (191.000000, 2.000000) mDisabled=0x00000000
01-09 13:21:51.422: D/PhoneStatusBar(139): panel: beginning to track the user's touch, y=2 opening=true
01-09 13:21:53.452: D/PhoneStatusBar(139): panel: ACTION_UP at (277.000000, 168.000000) mDisabled=0x00000000
01-09 13:21:53.462: D/PhoneStatusBar(139): gesture: vraw=(48.303123,-210.310593) vnorm=(48.303123,-210.310593) vlinear=-215.786316
01-09 13:21:53.462: D/PhoneStatusBar(139): panel: will fling, y=204 vel=-215.78632
01-09 13:21:54.012: W/InputManagerService(85): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@415cf110
01-09 13:21:54.412: I/ActivityManager(85): START {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher} from pid 85
01-09 13:21:54.412: W/WindowManager(85): Failure taking screenshot for (180x300) to layer 21050
01-09 13:21:54.452: W/NetworkManagementSocketTagger(85): setKernelCountSet(10004, 1) failed with errno -2
01-09 13:21:55.842: W/InputManagerService(85): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@415fb440 (uid=10040 pid=1004)
01-09 13:21:55.882: W/IInputConnectionWrapper(1004): showStatusIcon on inactive InputConnection
01-09 13:21:56.974: W/NetworkManagementSocketTagger(85): setKernelCountSet(10040, 0) failed with errno -2

我的清单有问题吗?:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

4

1 回答 1

2

您的应用程序需要设置 PendingIntent,这是 setLatestEventInfo() 所要求的。所以替换:

notification.setLatestEventInfo(this, msg, details, null);

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, msg, details, pendingIntent);

这将使您的应用程序工作,但此外,我强烈建议您在 Apilevels 之间进行区分。您的应用程序可能如下所示:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button) findViewById(R.id.button1);
        final TextView texty = (TextView) findViewById(R.id.textView2);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final EditText input2 = (EditText) findViewById(R.id.editText2);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String notificationmessage = input.getText().toString();
                String notificationdetails = input2.getText().toString();
                texty.setText("Notification Created");
                displayNotification(notificationmessage, notificationdetails);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /**
     * Show a notification while this service is running.
     * 
     */
    private void displayNotification(String msg, String details) {
        int num = 0;
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                intent, 0);
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion <= 8) {
            manager.notify(num,
                    createNotificationApi8(msg, details, pendingIntent));
        } else {
            manager.notify(num,
                    createNotificationApi14(msg, details, pendingIntent));
        }
    }

    @TargetApi(value = 14)
    private Notification createNotificationApi14(String msg, String details,
            PendingIntent intent) {
        Notification.Builder builder = new Builder(getApplicationContext());
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentTitle(msg);
        builder.setContentIntent(intent);
        builder.setContentInfo(details);
        Notification notification = builder.getNotification();
        return notification;
    }

    @TargetApi(value = 8)
    private Notification createNotificationApi8(String msg, String details,
            PendingIntent intent) {
        Notification notification = new Notification(R.drawable.ic_launcher,
                msg, System.currentTimeMillis());
        notification.setLatestEventInfo(this, msg, details, intent);
        return notification;
    }
}

我希望这对你有帮助,否则请告诉我。安达克

于 2013-01-09T22:26:36.893 回答