2

我为状态通知设置了一些代码,根据 eclipse 的代码没有任何问题。但是,当我启动它时,代码不会运行。那么它绕过通知并直接进入主要活动。为什么我的代码会跳过通知?

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class NoteMe extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int notifID = getIntent().getExtras().getInt("NotifID");
    Intent i = new Intent("com.example.something.MainActivity");

    PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification(R.drawable.ic_launcher,
            "STOP LOOK LISTEN!", System.currentTimeMillis());

    CharSequence message = "This is your alert, courtesy of the AlarmManager";
    notif.setLatestEventInfo(this, message, message, detailsIntent);
    notif.vibrate = new long[] { 100, 250, 100, 500 };
    nm.notify(notifID, notif);
    finish();
}

}

其次是清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.something"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />

<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>
            <activity android:name=".NoteMe" >
        <intent-filter>
            <action android:name="com.example.something.NoteMe" />
            <category android:name="android.intent.category.DEFAULT" /> 
        </intent-filter>
    </activity>
</application>

</manifest>

和日志猫!--其实logcat里面什么都没有(奇怪)

4

2 回答 2

1

在清单中,如果您<action android:name="android.intent.action.MAIN" />从 MainActivity 的意图过滤器中删除 并将其放入 NoteMe 活动的意图过滤器中,则该活动应该在启动时运行,而不是另一个。您只需在调用之前从 NoteMe 活动启动 MainActivity finish()

于 2012-07-18T17:37:49.647 回答
0

你的onCreate()方法不见了setContentView()。这应该在 之后调用super.onCreate(savedInstanceState)。这告诉 Android 您希望将哪个 XML 布局与Activity.

于 2012-07-18T17:09:27.377 回答