Email.java
package com.examp.notificationdemof;
import java.io.Serializable;
public class Email implements Serializable{
private String title,content;
public Email(String title,String content)
{
this.title=title;
this.content=content;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title=title;
}
public String getContent()
{
return content;
}
public void setContent(String content)
{
this.content=content;
}
}
EmailActivity.java
package com.examp.notificationdemof;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class EmailActivity extends Activity{
private TextView tvTitle=null;
private TextView tvContent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email);
tvTitle=(TextView)findViewById(R.id.tvTiTle);
tvContent=(TextView)findViewById(R.id.tvContent);
Bundle bundle=getIntent().getExtras();
if (bundle==null)
return;
Email email=(Email)bundle.getSerializable("email");
if (email!=null)
{
tvTitle.setText(email.getTitle());
tvContent.setText(email.getContent());
}
}
}
MainActivity.java
package com.examp.notificationdemof;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private NotificationManager notificationManager;
private Button bt1=null;
private Button bt2=null;
private final int NOTIFICATION_ID = 0x123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
bt1=(Button)findViewById(R.id.bt_send);
bt1.setOnClickListener(this);
bt2=(Button)findViewById(R.id.bt_cancel);
bt2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_send:
String title="Hello Android";
String content="welcome to the Android world";
Email email=new Email(title,content);
Intent intent=new Intent(MainActivity.this,EmailActivity.class);
Bundle bundle=new Bundle();
bundle.putSerializable("email", email);
intent.putExtras(bundle);
startActivity(intent);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Builder builder=new Notification.Builder(this);
builder.setTicker("You got a mail");
builder.setContentTitle(email.getTitle());
builder.setContentText(email.getContent());
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
Notification notification=builder.build();
notificationManager.notify(NOTIFICATION_ID,notification);
break;
case R.id.bt_cancel:
notificationManager.cancel(NOTIFICATION_ID);
break;
default:break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
這個是不是手機權限的問題,檢查一下通知權限。