一:Notification
1.NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2. notification.setLatestEventInfo(this, "國安部給你發短信!", "你被通輯啦~~~", contentIntent);
2.1: PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
2.1.1: Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:110"));
3.nm.notify(0,notification);
3.1:Notification notification = new
Notification(R.drawable.ic_launcher,"有新的消息到來了",System.currentTimeMillis());
4.為避免成為流氓軟件,要: notification.flags = Notification.FLAG_AUTO_CANCEL;
詳細代碼:
- public class MainActivity extends Activity {
- private NotificationManager nm;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- }
- // 通知是顯示在一個系統應用里面的, 如果系統應用ui掛了,通知就不會出來了。
- public void click(View view) {
- @SuppressWarnings("deprecation")
- Notification notification = new
- Notification(R.drawable.ic_launcher,"有新的消息到來了",System.currentTimeMillis());
- notification.flags = Notification.FLAG_AUTO_CANCEL;
- // //延期的意圖,不是立刻執行的意圖,最終這個意圖是在另外一個應用程序里面執行
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_CALL);
- intent.setData(Uri.parse("tel:110"));
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
- //舊的api,一定要記得設置notification這一個參數
- notification.setLatestEventInfo(this, "國安部給你發短信!", "你被通輯啦~~~", contentIntent);
- nm.notify(0,notification);
- }
- }
運行結果:
二:樣式:style(只能作用在控件上)
1.在res/values/styles.xml設置樣式
<style name="text_content_style">
<!-- item里面是鍵值對! -->
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">20sp</item>
</style>
繼承形式一
<style name="text_title_style" parent="@style/text_content_style">
<item name="android:textSize">30sp</item>
</style>
繼承形式二
<style name="text_subtitle_style" parent="@style/text_content_style">
<item name="android:textSize">25sp</item>
</style>
2.使用樣式:
<TextView
style="@style/text_title_style"
android:text="我是標題"/>
<TextView
style="@style/text_subtitle_style"
android:text="我是sub標題"/>
<TextView
style="@style/text_content_style"
android:text="我是文本"/>
3.效果圖:
三:主題theme(作用在Activity或整個應用程序上Application)
1.在style.xml中配置:
參考:~\adt-bundle-windows-x86-20130729\sdk\platforms\android-16\data\res\values\themes.xml
第162行:Window attributes,可配置自己的Window attribute的主題。
注意:不同於系統的,自己要定義需要在name的字符串前加上:android: 如:
系統:
<item name="windowBackground">@android:drawable/screen_background_selector_dark</item>
自己:
<item name="android:windowBackground">@color/red</item>
<style name="red_theme">
<item name="android:windowBackground">@color/red</item>
</style>
2.在AndroidMainfest.xml中使用:
<activity
android:theme="@style/red_theme"
...
效果圖: