我为Status Notification设置了一些代码,根据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:
<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! - 实际上logcat(Strange)中没有任何东西
答案 0 :(得分:1)
在清单中,如果从MainActivity的intent过滤器中删除<action android:name="android.intent.action.MAIN" />
并将其放入NoteMe活动的intent过滤器中,则该活动应在启动时运行而不是在另一个上运行。您只需在致电finish()
之前从NoteMe活动中启动MainActivity。
答案 1 :(得分:0)
您的onCreate()
方法遗失setContentView()
。这应该在super.onCreate(savedInstanceState)
之后调用。这告诉Android您想要与Activity
建立哪种XML布局。