MainActivity是項目應用的主類
/**
* @author Freedom
* @2014 October 19th
*
*/
public class MainActivity extends Activity {
// 聲明Notification(通知)的管理者
private NotificationManager mNotifyMgr;
// 聲明Notification(通知)對象
private Notification notification;
// 消息的唯一標示id
public static final int mNotificationId = 001;
// 聲明一個啓動按鈕、關閉按鈕
private Button startbtn, cancelbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startbtn = (Button) findViewById(R.id.start);
cancelbtn = (Button) findViewById(R.id.cancel);
// 綁定startbtn按鈕的onclick事件
startbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 創建一個即將要執行的PendingIntent對象
Intent resultIntent = new Intent(MainActivity.this,
ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// 建立所要創建的Notification的配置信息,並有notifyBuilder來保存。
notification = new Notification.Builder(MainActivity.this)
// 觸摸之後,通知立即消失
.setAutoCancel(true)
// 顯示的時間
.setWhen(System.currentTimeMillis())
// 設置通知的小圖標
.setSmallIcon(R.drawable.smallicon)
// 設置狀態欄顯示的文本
.setTicker("狀態欄提示消息")
// 設置通知的標題
.setContentTitle("通知的標題!")
// 設置通知的內容
.setContentText("通知的內容!")
// 設置聲音(系統默認的)
// .setDefaults(Notification.DEFAULT_SOUND)
// 設置聲音(自定義)
.setSound(
Uri.parse("android.resource://org.crazyit.ui/"
+ R.raw.msg))
// 設置跳轉的activity
.setContentIntent(resultPendingIntent).build();
// 創建NotificationManager對象,併發布和管理所要創建的Notification
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, notification);
}
});
// 綁定cancelbtn按鈕的onclick事件
cancelbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mNotifyMgr.cancel(mNotificationId);
}
});
}
</pre><p><span style="font-family: 'Microsoft YaHei';"><span style="font-size:18px;"><strong>跳轉之後的ResultActivity</strong></span></span></p><p></p><pre code_snippet_id="490135" snippet_file_name="blog_20141020_2_7484839" name="code" class="java"><span style="font-size:18px;"><strong>/**
*
* @author Freedom
* @2014 October 19th
*
*/
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
}</strong></span>
mainactivity的佈局文件
<span style="font-size:18px;"><strong><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/startbtntext"
/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancelbtntext"
/>
</LinearLayout></strong></span>
resultactivity的佈局文件
<span style="font-size:18px;"><strong><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android"
android:scaleType="fitXY"
android:contentDescription="@string/desciption"
/>
</LinearLayout>
</strong></span>
Manifest文件
<span style="font-size:18px;"><strong><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mylatestnotifiction"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mylatestnotifiction.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="com.example.mylatestnotifiction.ResultActivity"
android:label="第二個頁面"
></activity>
</application>
</manifest></strong></span>