Android 通知类未加载
我刚刚开始在安卓上做实验。我正在读一本由Wrox pulication公司开发的Android 4应用程序的入门书。有一个显示通知的代码。问题是我写的代码(不是复制的)修改很少。因此,我可以在状态/通知栏上显示通知,但单击通知时,不会调用notificaion类。以下是android_mainfest的代码Android 通知类未加载,android,Android,我刚刚开始在安卓上做实验。我正在读一本由Wrox pulication公司开发的Android 4应用程序的入门书。有一个显示通知的代码。问题是我写的代码(不是复制的)修改很少。因此,我可以在状态/通知栏上显示通知,但单击通知时,不会调用notificaion类。以下是android_mainfest的代码 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="legacy_systems
<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>
而且,Notification.java的代码是
打包遗留系统通知项目
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);
}
}
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>
建议您不要将自己的类命名为与系统定义类相同的名称。您正试图将android.app.Notification作为活动打开,该活动将不起作用,并将在LogCat中产生警告
请将您的
通知
活动重命名为其他活动,例如Easdluaerdf
,以使其唯一,调整您的清单和挂起内容
以匹配,您的运气会更好。在清单中写入android:name=“legacy_systems.notificationproject.Notification”如何
而不仅仅是通知
?还有,你的应用程序崩溃了吗?如果有,则发布stack trace.Nope。应用程序不会崩溃。什么也没发生。我点击通知,它就停留在那里。至于更改名称,它不起作用,我做了家庭作业。我建议您切换到Notification.Builder
(或Android支持库中的NotificationCompat.Builder
)。下面是一个示例项目,演示如何使用生成器显示通知
:@commonware我同意使用生成器
s,但您是否发现上述代码有任何错误?虽然不推荐使用setLatestEventInfo()
,但它仍然可以工作。我也这么做了。我重新命名了它。尽管如此,通知仍然存在。单击通知时,不会发生任何事情。