通知类没有加载

时间:2013-02-09 19:37:11

标签: android

我刚刚开始尝试Android。我正在阅读通过Wrox pulication开始Android 4应用程序开发的书。有一个显示通知的代码。问题是我编写的代码(未复制)很少修改。因此,我能够在状态/通知栏上显示通知,但是在点击通知时,不会调用通知类。这是android_mainfest的代码。

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

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.VIBRATE"/>

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name="Notification"
        android:label="Details of the Notification" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />                    
        </intent-filter>
    </activity>
</application>

</manifest>

现在,MainActivity的代码 package legacy_systems.notificationproject;

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

public class MainActivity extends Activity {
int notificationID = 1;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View V)
{
    getNotification();
}

protected void getNotification()
{
    Intent i = new Intent(this, Notification.class);
    i.putExtra("notificationID", notificationID);
    PendingIntent pn = PendingIntent.getActivity(this, 0, i, 0);
    NotificationManager nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification n = new Notification(R.drawable.ic_launcher,
                                      "Reminder! Meeting starts in 5 Minute",
                                      System.currentTimeMillis());
    CharSequence from = "System Alarm";
    CharSequence message = "Meeting with customer in next 2 minute";
    n.setLatestEventInfo(this, from, message, pn);
    n.vibrate = new long[]{ 100,250,50,500};
    nm.notify(notificationID, n);       
}

}

而且,Notification.java的代码是     package legacy_systems.notificationproject;

import android.app.Activity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.util.Log;

public class Notification extends Activity{
String tag;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Log.d(tag,"In here");

    setContentView(R.layout.notification);

    NotificationManager nm = (NotificationManager)getSystemService(VIBRATOR_SERVICE);
    nm.cancel(getIntent().getExtras().getInt("notificationID"));
}
}

而activity_main.xml是,

<xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btn"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="@string/getnot"
    android:onClick="onClick"
 />

</RelativeLayout>    

1 个答案:

答案 0 :(得分:0)

建议您不要将自己的类命名为与系统定义的类相同的名称。您正在尝试将android.app.Notification作为活动打开,这不起作用,应该在LogCat中产生警告。

请将您的Notification活动重命名为其他内容,例如Easdluaerdf,以使其独一无二,调整您的清单并PendingIntent匹配,您应该有更好的运气。