该应用会发布通知,但是当您点击它时,没有任何反应。
//this is a Notification app
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
static final int NOTIFICATION_ID = 0x1123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Button bn1 = (Button)this.findViewById(R.id.button1);
bn1.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification notif = new Notification();
notif.icon = R.drawable.notify;
notif.tickerText = "启动其他Activity的通知";
notif.when = System.currentTimeMillis();
notif.defaults = Notification.DEFAULT_SOUND;
notif.defaults = Notification.DEFAULT_ALL;
notif.setLatestEventInfo(MainActivity.this,"普通通知","点击查看", pending);
NotificationManager nfm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nfm.notify(NOTIFICATION_ID, notif);
}
});
Button bn2 = (Button)this.findViewById(R.id.button2);
bn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
NotificationManager nfm = (NotificationManager)MainActivity.this.getSystemService(NOTIFICATION_SERVICE);
nfm.cancel(NOTIFICATION_ID);
}
});
}
}
//the OtherActivity.java
package com.myapp.notification;
import android.os.Bundle;
import android.app.Activity;
public class OtherActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other)
}
}
// sure! the xml layout file is no problem */
答案 0 :(得分:0)
移动Button bn1 =(Button)this.findViewById(R.id.button1); bn1.setOnClickListener(new View.OnClickListener(){
以及onCreateView方法