按下Notification進入到另外一個Activity
1). 在Activity中設置點擊按鈕觸發的事件如下
宣告Intent所要執行的java檔(mService.java),並啟動Service
2). mService.java檔中宣告Service的lifecycle,分別是onCreate(),onStart(),onDestory
宣告handler,管理新開的thread,名為task的Runnable
宣告Runnable,執行所要做的Notification method
3). 撰寫開啟Notification Manager的method
設定要跳去哪一個Activity
參考[Android] [JAVA] Notification & PendingIntent
4). onCreate()繼承原有的onCreate()
onStart()中寫好延遲2秒去執行task,接著結束此Service,使lifecycle進入onDestory()
onDestory()繼承原有的onDestory()
5). Runnable中去執行Notification的method
//=============================================================
[string.xml]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, DeviceInfo1!</string>
<string name="app_name">DeviceInfo2</string>
<string name="android_version">Android Version</string>
<string name="kernel_version">Kernel Version</string>
<string name="model_number">Model Number</string>
<string name="product_sku">Product Sku</string>
<string name="image_version">Image Version</string>
<string name="ec_version">EC Version</string>
<string name="cpu_vendor">CPU Vendor</string>
<string name="cpu_speed">CPU Speed</string>
<string name="cpu_version">CPU Version</string>
<string name="info_submit">Submit</string>
<string name="main_text">Please select the function as list. </string>
<string name="main_text_info1">Start service and notification bar after 2 senconds.
Then it will change activity to DeviceInfo.</string>
<string name="main_submit_info1">1. Device Info</string>
</resources>
//-------------------------------------------------------------------------------------------------------------
[info.xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="219dp"
android:layout_height="691dp"
android:orientation="vertical" >
<CheckBox
android:id="@+id/checkBox1_android_version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/android_version" />
<CheckBox
android:id="@+id/checkBox2_kernel_version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/kernel_version" />
<CheckBox
android:id="@+id/checkBox3_model_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/model_number" />
<CheckBox
android:id="@+id/checkBox4_product_sku"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/product_sku" />
<CheckBox
android:id="@+id/checkBox5_image_version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/image_version" />
<CheckBox
android:id="@+id/checkBox6_ec_version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ec_version" />
<CheckBox
android:id="@+id/checkBox7_cpu_vendor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cpu_vendor" />
<CheckBox
android:id="@+id/checkBox8_cpu_speed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cpu_speed" />
<CheckBox
android:id="@+id/checkBox9_cpu_version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cpu_version" />
<Button
android:id="@+id/submit"
android:layout_width="191dp"
android:layout_height="wrap_content"
android:text="@string/info_submit" />
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
</AbsoluteLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/result"
android:layout_width="1015dp"
android:layout_height="match_parent"
android:text="" />
</LinearLayout>
</LinearLayout>
//-------------------------------------------------------------------------------------------------------------
[main.xml]
<?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" >
<TextView
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.02"
android:orientation="vertical" >
<Button
android:id="@+id/main_submit_info1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_submit_info1" />
</LinearLayout>
<LinearLayout
android:layout_width="1028dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/main_text_info1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="@string/main_text_info1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
//-------------------------------------------------------------------------------------------------------------
[DeviceInfo2.java]
package com.deviceinfo2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class DeviceInfo2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 找到元件資源
TextView text_main=(TextView)findViewById(R.id.main_text);
TextView text_main_info1=(TextView)findViewById(R.id.main_text_info1);
Button button_info1 = (Button) findViewById(R.id.main_submit_info1);
// 設定title字體大小
text_main.setTextSize(25);
text_main_info1.setTextSize(20);
// 設定按鍵觸發的method
button_info1.setOnClickListener(listDeviceInfo);
}
private Button.OnClickListener listDeviceInfo = new Button.OnClickListener() {
@Override
public void onClick(View v) {
// Toast
Toast.makeText(v.getContext(), "Please wait for notification about 2 senconds",
Toast.LENGTH_LONG).show();
// 宣告intent,並指定要啟動的class
Intent intent = new Intent(DeviceInfo2.this, mService.class);
// 以startservice方式啟動intent
startService(intent);
}
};
}
//-------------------------------------------------------------------------------------------------------------
[mService.java]
package com.deviceinfo2;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class mService extends Service {
private Handler handle = new Handler();
private int count=0;
@Override
public void onCreate() {
Log.d("TAG", "onCreate");
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void onStart(Intent intent, int startId) {
Log.d("TAG", "onStart");
handle.postDelayed(tasks, 2000);
// 停掉自己這個服務
mService.this.stopSelf();
}
private Runnable tasks = new Runnable() {
@Override
public void run() {
Log.d("TAG", "Runnable");
Log.d("RUN", "RunCount: " + Integer.toString(count++));
notification();
handle.removeCallbacks(tasks);
}
};
private void notification(){
Log.d("TAG", "Notification");
// 取得Notification服務
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// 設定當按下這個通知之後要執行的activity
Intent notifyIntent = new Intent(mService.this,
DeviceInfoActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appIntent = PendingIntent.getActivity(
mService.this, 0, notifyIntent, 0);
Notification notification = new Notification();
// 設定出現在狀態列的圖示
notification.icon = R.drawable.ic_launcher;
// 顯示在狀態列的文字
notification.tickerText = "notification on status bar.";
// Notification被點擊後,便消失
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// 會有通知預設的鈴聲、振動、light
notification.defaults |= Notification.DEFAULT_ALL;
// 設定通知的標題、內容
notification.setLatestEventInfo(mService.this, "DeviceInfo",
"Checkoout the infomation of device", appIntent);
// 送出Notification
notificationManager.notify(0, notification);
}
public void onDestroy() {
Log.d("TAG", "onDestory");
super.onDestroy();
}
}
//-------------------------------------------------------------------------------------------------------------
[DeviceInfoActicity.java]
package com.deviceinfo2;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.util.Log;
import android.view.View;
import android.os.SystemProperties;
public class DeviceInfoActivity extends Activity {
private CheckBox checkBox1_android_version, checkBox2_kernel_version,
checkBox3_model_number, checkBox4_product_sku,
checkBox5_image_version, checkBox6_ec_version,
checkBox7_cpu_vendor, checkBox8_cpu_speed, checkBox9_cpu_version;
private Button button;
private TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
// 取得資源類別中的介面元件
checkBox1_android_version = (CheckBox) findViewById(R.id.checkBox1_android_version);
checkBox2_kernel_version = (CheckBox) findViewById(R.id.checkBox2_kernel_version);
checkBox3_model_number = (CheckBox) findViewById(R.id.checkBox3_model_number);
checkBox4_product_sku = (CheckBox) findViewById(R.id.checkBox4_product_sku);
checkBox5_image_version = (CheckBox) findViewById(R.id.checkBox5_image_version);
checkBox6_ec_version= (CheckBox) findViewById(R.id.checkBox6_ec_version);
checkBox7_cpu_vendor = (CheckBox) findViewById(R.id.checkBox7_cpu_vendor);
checkBox8_cpu_speed = (CheckBox) findViewById(R.id.checkBox8_cpu_speed);
checkBox9_cpu_version = (CheckBox) findViewById(R.id.checkBox9_cpu_version);
button = (Button) findViewById(R.id.submit);
result = (TextView) findViewById(R.id.result);
// button元件要偵聽動作及觸發執行的方法名稱
button.setOnClickListener(listInfo);
}
private Button.OnClickListener listInfo = new Button.OnClickListener() {
@Override
public void onClick(View v) {
result.setText("");
result.setTextSize(20);
if (checkBox1_android_version.isChecked()) {
result.append("Android Version: "
+ SystemProperties.get("ro.build.version.release")
+ "\n\n");
}
if (checkBox2_kernel_version.isChecked()) {
Properties props = System.getProperties();
result.append("Kernel Version: "
+ props.getProperty("os.version") + "\n\n");
}
if (checkBox3_model_number.isChecked()) {
result.append("Model Number: "
+ SystemProperties.get("ro.product.model") + "\n\n");
}
if (checkBox4_product_sku.isChecked()) {
result.append("Product Sku: "
+ SystemProperties.get("ro.dinfo.sku") + "\n\n");
}
if (checkBox5_image_version.isChecked()) {
result.append("Image Version: "
+ SystemProperties.get("ro.build.display.id") + "\n\n");
}
if (checkBox6_ec_version.isChecked()) {
result.append("EC Version: " + getECVer() + "\n");
}
if (checkBox7_cpu_vendor.isChecked()) {
result.append("CPU Vendor: "
+ SystemProperties.get("ro.cpu.vendor") + "\n\n");
}
if (checkBox8_cpu_speed.isChecked()) {
result.append("CPU Speed: "
+ SystemProperties.get("ro.cpu.speed") + "\n\n");
}
if (checkBox9_cpu_version.isChecked()) {
result.append("CPU Version: "
+ SystemProperties.get("ro.cpu.version") + "\n\n");
}
}
};
private String getECVer() {
String str = "";
int len = 0;
char[] cbuf = new char[200];
try {
FileReader fn = new FileReader("EC路徑");
try {
while ((len = fn.read(cbuf, 0, cbuf.length)) >= 0) {
Log.d("***", str);
str += String.valueOf(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return str;
}
}
//-------------------------------------------------------------------------------------------------------------
[AndroidManifest.xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deviceinfo2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".DeviceInfo2"
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="DeviceInfoActivity" >
</activity>
<service
android:name=".mService"
android:exported="true"
android:process=":remote" >
</service>
</application>
</manifest>
沒有留言:
張貼留言