0

我刚刚开始在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 包 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>    
4

1 回答 1

0

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

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

于 2013-02-09T20:14:57.073 回答