要点概括
1、文件管理器,AlertDialog
2、Service应用,Notification通知栏,防止Service被杀,点击通知栏返回后台程序,(默认会创建新的Acticity)
3、Brodcast广播的应用
4、Activity之间的滑动切换
5、滑动侧边栏的实现,slideMenu类
6、响应实体按键,返回键使程序后台
7、程序被任务管理器强杀时的安全退出
8、Activity状态保存
9、歌词文件的解析与同步
10、在一个应用程序中启动另外一个已经安装的应用程序或系统程序
11、自定义PopupWindow菜单
(一)文件管理器
使用View界面布局生成对话框 AlertDialog dialog = new AlertDialog.Builder(this).setTitle("选择音乐目录").setView(viewDlg).create();
*****其中dialog不能多次调用setview(),否则会产生父视图已经包含子视图的异常。
View由含有ListView的layout布局文件生成 viewDlg = LayoutInflater.from(this).inflate(R.layout.filedialog, null);
ListView 设置条目点击监听器 listview.setOnItemClickListener
通过SimpleAdapter将List<Map<String,String>>关联到ListView.setAdapter();
(二)Service应用
在AndroidManifest.xml中注册Service
<service
android:name=".service.PlayerService"
android:enabled="true"
android:exported="true" >
</service>
class PlayerService extends Service 需要继承Service,并重写onStartCommand()方法,调用startService(Intent);启动Service,进入onStartCommand()。
提高Service的生存能力
(1)在AndroidMainifest.xml文件中,主Activity中设置优先级
<activity
android:name=".PlayerActivity"
android:label="@string/app_name">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
(2)通过添加通知栏将Service设置成前台模式运行,使用 startForeground(0x111, notification);
生成通知栏
Notification notification = new Notification(R.drawable.notif_ico,
getString(R.string.app_name), System.currentTimeMillis()); 第一个参数为通知栏图标,第二行为通知栏名字
PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
new Intent(this, PlayerActivity.class), 0); PendingIntent是一种“待命Intent”,在需要的时候由使用者调用
notification.setLatestEventInfo(this, FileUtil.getNameFromPath(mp3Path), "Monkey Music",
pendingintent); 第一个参数为程序上下文,第二个参数为下拉栏里面第一行显示内容,第三个参数为下拉栏第二行的内容
(3)设置Service标志START_STICKY,用于Service被强杀后的重启
flags = START_STICKY;
(4)如果设置了startForeground();必须在onDestory()中使用 stopForeground(true);进行退出处理。
(三)Brodcast广播的应用
1.继承BroadcastReceiver,并重写onReceive()方法。
class LrcBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//获取当前歌词文本,更新UI
String lrcMsg = intent.getStringExtra("lrcmsg");
textView.setText(lrcMsg);
}
2.注册/解除广播
注册前需要设置IntentFilter过滤动作,IntentFilter.addAction(String),其中String为自定义的动作
registerReceiver(BroadcastReceiver, IntentFilter);
unregisterReceiver(BroadcastReceiver);
3.发送广播
sendBroadcast(Intent);
(四)Activity之间的滑动切换
(2)activity对应的layout绑定触摸和手势监听器
mGestureDetector = new GestureDetector((OnGestureListener) this);
LinearLayout viewSnsLayout = (LinearLayout)findViewById(R.id.playerLay);
viewSnsLayout.setOnTouchListener(this);
viewSnsLayout.setLongClickable(true);
(3)并实现OnFling()方法,在onFling()方法中发送startActivity(Intent)启动其他Acticity
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)方法
(五)滑动侧边栏
使用一个继承自ViewGroup的类SlidingMenu,使用其构造函数SlidingMenu(Context context, View main, View menu)
将两个layout布局通过View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);合并成一个整体的View
在主Activity中的onCreate(Bundle savedInstanceState)中,使用setContentView(mSlidingMenu),将整合的mSliingMenu视图设置为主视图。
****menu视图中控件的查找可以使用listview = (ListView)menuView.findViewById(R.id.listview);
也可以调用SlidingMenu的mSlidingMenu.open()和close()方法打开和关闭侧边栏。
(六)响应实体按键,返回键使程序后台
重写Activity的onKeyDown(int keyCode, KeyEvent event)方法,
判断event = KeyEvent.KEYCODE_BACK时,使用 moveTaskToBack(false);使程序进入后台运行
(七)程序被任务管理器强杀时的安全退出
****一般程序必须添加在主Activity按返回键后台功能,否则会自动销毁程序,如果仍残留Service等其他服务,会造成无法正常清除Service,造成重启错误。
因为任务管理器在强杀时会调用现存主页Acticity的onDestory()方法,如果没有Activity存留,则无法在onDestory()中进行安全清除*******
重写onDestory()方法,在onDestory()方法中进行清除工作。
(八)Activity数据共享
(1)创建Java数据共享类,设置static成员变量和方法
当一个应用有多个Activity时,当某个Activity需要用到另外一个activity的数据时需要,可以使用Java数据共享类调用。
(九)歌词文件的解析与同步
使用广播机制同步发送歌词
(1)将歌词文件通过提取分成时间和对应的歌词内容,分别用两个队列保存。
Queue<Long> timeMills = new LinkedList<Long>();
Queue<String> messages = new LinkedList<String>();
(2)内部类实现Runnable接口
class UpdateTimeCallback implements Runnable{},实现run()方法
通过判断播放起始时间和当前时间的差值,查找时间队列,匹配后取出对应的歌词(注意暂停后的处理),使用sendBroadcast(Intent)发送
同时在run()方法中使用 handler.postDelayed(updateTimeCallback,10);第一个参数为Runnable实现类,第二个为多长时间后发送。
进行循环运行run()方法。实现同步歌词
需要停止循环时, 使用handler.removeCallbacks(updateTimeCallback);进行移除。
(十)在一个应用程序中启动另外一个已经安装的应用程序或系统程序
可以通过ComponentName以及Intent的setComponent来实现
//组件名称,第一个参数是应用程序的包名,后一个是这个应用程序的主Activity
ComponentName com = new ComponentName("com.antroid.Test", "com.antroid.Test.TestActivity");
Intent intent = new Intent();
//设置部件
intent.setComponent(com);
startActivity(intent);
(十一)自定义PopupWindow菜单
菜单布局menu.xml
通过布局转化视图View,通过View创建PopupWindow,并设置控件监听器
通过InitWindow()设置好相关属性和dismiss()
通过 popupWindow.showAtLocation(mainView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL ,0,0);//根据mainview确定位置(在mainView当中)
和popupWindow.showAsDropDown(mainView,0,0);//根据mainview确定位置(在mainview父窗口)
最后通过判断isShowing()创建和关闭窗口。
可以设置背景透明和打开窗口背景变暗
//设置透明背景
ColorDrawable cd = new ColorDrawable(0x000000);
popupWindow.setBackgroundDrawable(cd);
//产生背景变暗效果
WindowManager.LayoutParams lp;
lp=getWindow().getAttributes();
lp.alpha = 0.4f;
getWindow().setAttributes(lp);
在dismiss中取消变暗
public void onDismiss() {
// TODO Auto-generated method stub
// WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
1、文件管理器,AlertDialog
2、Service应用,Notification通知栏,防止Service被杀,点击通知栏返回后台程序,(默认会创建新的Acticity)
3、Brodcast广播的应用
4、Activity之间的滑动切换
5、滑动侧边栏的实现,slideMenu类
6、响应实体按键,返回键使程序后台
7、程序被任务管理器强杀时的安全退出
8、Activity状态保存
9、歌词文件的解析与同步
10、在一个应用程序中启动另外一个已经安装的应用程序或系统程序
11、自定义PopupWindow菜单
(一)文件管理器
使用View界面布局生成对话框 AlertDialog dialog = new AlertDialog.Builder(this).setTitle("选择音乐目录").setView(viewDlg).create();
*****其中dialog不能多次调用setview(),否则会产生父视图已经包含子视图的异常。
View由含有ListView的layout布局文件生成 viewDlg = LayoutInflater.from(this).inflate(R.layout.filedialog, null);
ListView 设置条目点击监听器 listview.setOnItemClickListener
通过SimpleAdapter将List<Map<String,String>>关联到ListView.setAdapter();
(二)Service应用
在AndroidManifest.xml中注册Service
<service
android:name=".service.PlayerService"
android:enabled="true"
android:exported="true" >
</service>
class PlayerService extends Service 需要继承Service,并重写onStartCommand()方法,调用startService(Intent);启动Service,进入onStartCommand()。
提高Service的生存能力
(1)在AndroidMainifest.xml文件中,主Activity中设置优先级
<activity
android:name=".PlayerActivity"
android:label="@string/app_name">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
(2)通过添加通知栏将Service设置成前台模式运行,使用 startForeground(0x111, notification);
生成通知栏
Notification notification = new Notification(R.drawable.notif_ico,
getString(R.string.app_name), System.currentTimeMillis()); 第一个参数为通知栏图标,第二行为通知栏名字
PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
new Intent(this, PlayerActivity.class), 0); PendingIntent是一种“待命Intent”,在需要的时候由使用者调用
notification.setLatestEventInfo(this, FileUtil.getNameFromPath(mp3Path), "Monkey Music",
pendingintent); 第一个参数为程序上下文,第二个参数为下拉栏里面第一行显示内容,第三个参数为下拉栏第二行的内容
(3)设置Service标志START_STICKY,用于Service被强杀后的重启
flags = START_STICKY;
(4)如果设置了startForeground();必须在onDestory()中使用 stopForeground(true);进行退出处理。
(三)Brodcast广播的应用
1.继承BroadcastReceiver,并重写onReceive()方法。
class LrcBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//获取当前歌词文本,更新UI
String lrcMsg = intent.getStringExtra("lrcmsg");
textView.setText(lrcMsg);
}
2.注册/解除广播
注册前需要设置IntentFilter过滤动作,IntentFilter.addAction(String),其中String为自定义的动作
registerReceiver(BroadcastReceiver, IntentFilter);
unregisterReceiver(BroadcastReceiver);
3.发送广播
sendBroadcast(Intent);
(四)Activity之间的滑动切换
方法一:
(1) 设置手势检测器GestureDetector,重写OnFling()方法.
GestureDetector mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { } }; (2)重写Activity的onTouchEvent方法,绑定GestureDetector处理触摸事件.方法二:@Override public boolean onTouchEvent(MotionEvent event) { mGestureDetector.onTouchEvent(event); return super.onTouchEvent(event); }
(1)Activity实现OnTouchListener和OnGestureListener接口,
public class PlayerActivity extends Activity implements OnTouchListener,OnGestureListener {}(2)activity对应的layout绑定触摸和手势监听器
mGestureDetector = new GestureDetector((OnGestureListener) this);
LinearLayout viewSnsLayout = (LinearLayout)findViewById(R.id.playerLay);
viewSnsLayout.setOnTouchListener(this);
viewSnsLayout.setLongClickable(true);
(3)并实现OnFling()方法,在onFling()方法中发送startActivity(Intent)启动其他Acticity
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)方法
(五)滑动侧边栏
使用一个继承自ViewGroup的类SlidingMenu,使用其构造函数SlidingMenu(Context context, View main, View menu)
将两个layout布局通过View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);合并成一个整体的View
在主Activity中的onCreate(Bundle savedInstanceState)中,使用setContentView(mSlidingMenu),将整合的mSliingMenu视图设置为主视图。
****menu视图中控件的查找可以使用listview = (ListView)menuView.findViewById(R.id.listview);
也可以调用SlidingMenu的mSlidingMenu.open()和close()方法打开和关闭侧边栏。
(六)响应实体按键,返回键使程序后台
重写Activity的onKeyDown(int keyCode, KeyEvent event)方法,
判断event = KeyEvent.KEYCODE_BACK时,使用 moveTaskToBack(false);使程序进入后台运行
(七)程序被任务管理器强杀时的安全退出
****一般程序必须添加在主Activity按返回键后台功能,否则会自动销毁程序,如果仍残留Service等其他服务,会造成无法正常清除Service,造成重启错误。
因为任务管理器在强杀时会调用现存主页Acticity的onDestory()方法,如果没有Activity存留,则无法在onDestory()中进行安全清除*******
重写onDestory()方法,在onDestory()方法中进行清除工作。
(八)Activity数据共享
(1)创建Java数据共享类,设置static成员变量和方法
当一个应用有多个Activity时,当某个Activity需要用到另外一个activity的数据时需要,可以使用Java数据共享类调用。
(九)歌词文件的解析与同步
使用广播机制同步发送歌词
(1)将歌词文件通过提取分成时间和对应的歌词内容,分别用两个队列保存。
Queue<Long> timeMills = new LinkedList<Long>();
Queue<String> messages = new LinkedList<String>();
(2)内部类实现Runnable接口
class UpdateTimeCallback implements Runnable{},实现run()方法
通过判断播放起始时间和当前时间的差值,查找时间队列,匹配后取出对应的歌词(注意暂停后的处理),使用sendBroadcast(Intent)发送
同时在run()方法中使用 handler.postDelayed(updateTimeCallback,10);第一个参数为Runnable实现类,第二个为多长时间后发送。
进行循环运行run()方法。实现同步歌词
需要停止循环时, 使用handler.removeCallbacks(updateTimeCallback);进行移除。
(十)在一个应用程序中启动另外一个已经安装的应用程序或系统程序
可以通过ComponentName以及Intent的setComponent来实现
//组件名称,第一个参数是应用程序的包名,后一个是这个应用程序的主Activity
ComponentName com = new ComponentName("com.antroid.Test", "com.antroid.Test.TestActivity");
Intent intent = new Intent();
//设置部件
intent.setComponent(com);
startActivity(intent);
(十一)自定义PopupWindow菜单
菜单布局menu.xml
通过布局转化视图View,通过View创建PopupWindow,并设置控件监听器
通过InitWindow()设置好相关属性和dismiss()
通过 popupWindow.showAtLocation(mainView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL ,0,0);//根据mainview确定位置(在mainView当中)
和popupWindow.showAsDropDown(mainView,0,0);//根据mainview确定位置(在mainview父窗口)
最后通过判断isShowing()创建和关闭窗口。
可以设置背景透明和打开窗口背景变暗
//设置透明背景
ColorDrawable cd = new ColorDrawable(0x000000);
popupWindow.setBackgroundDrawable(cd);
//产生背景变暗效果
WindowManager.LayoutParams lp;
lp=getWindow().getAttributes();
lp.alpha = 0.4f;
getWindow().setAttributes(lp);
在dismiss中取消变暗
public void onDismiss() {
// TODO Auto-generated method stub
// WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}