Notification应用 博客分类: android androidnotification
程序员文章站
2024-03-14 22:31:11
...
package com.example.notificationtest; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity extends Activity { NotificationManager notificationManager; Spinner spinner; String[] arr = { "请选择", "face", "folder", "other", "text", "why" }; ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); spinner = (Spinner) findViewById(R.id.spinner); adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arr); adapter.setDropDownViewResource(R.layout.text); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (position == 1) { setNotifi(R.drawable.face, arr[1]); } if (position == 2) { setNotifi(R.drawable.folder, arr[2]); } if (position == 3) { setNotifi(R.drawable.other, arr[3]); } if (position == 4) { setNotifi(R.drawable.txt, arr[4]); } if (position == 5) { setNotifi(R.drawable.why, arr[5]); } } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); } private void setNotifi(int id, String text) { Intent intent = new Intent(this, Test.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pend = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); Notification notification = new Notification(); notification.icon = id; notification.tickerText = text; notification.defaults = Notification.DEFAULT_SOUND; notification.flags = Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(MainActivity.this, "MSN", text, pend); notificationManager.notify(0, notification); } @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; } }
推荐阅读
-
android 用户权限 博客分类: android androiduser-permission
-
Android自定义view(一) 博客分类: Android UI 界面 Android自定义view步骤
-
LayoutInflater和inflate()方法的用法 博客分类: android androidLayoutInflater
-
wifi开启关闭 博客分类: android androidwifi
-
android 用户权限 博客分类: android androiduser-permission
-
FrameLayout 博客分类: Android UI 界面
-
android应用创建快捷方式 博客分类: android android桌面创建快捷方式
-
显示所有应用程序列表,点击进入 博客分类: android android应用连接跳转
-
android的用户定位---location使用 博客分类: android androidloationManagergps
-
广播机制的2种注册方式 博客分类: android android广播机制