需要widget的基本知识
App Widgets 桌面小工具制作效果图
1. AndroidManifest.xml中添加
<receiver android:name="hb30.com.mob.Hb30WidgetProvider" > <intent-filter > <!-- 可以配置其它的Action,但这个是必须的 --> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/></intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget"/> </receiver>
2. 在res目录下添加xml目录,并建立刚才所需文件
3. 建立所需的布局文件。
4. 类文件,继承AppWidgetProvider类
创建PendingIntent的方法
a) getActivity(Context context,int requestCode,Intent intent,int flags) b) getBroadcast(Context context,int requestCode,Intent intent,int flags) c) getService(Context context,int requestCode,Intent intent,int flags)
RemoteViews的作用
a) 表示了一系列的View对象
b) 表示的对象运行在另外的进程当中
向App widget中加入Button
注意:App Widget当中的view运行在HomeScreen进程当中,所以绑定方式
不同:复写方法
接收来自appWidget的广播
1. 在AndroidManifest.xml中为AppWidgetProvider注册新的intent-filter; (它本身配置就是receiver,只要加filter就行)
2. 使用getBroadcast()方法创建一个PendingIntent;
3. 为AppWidget当中的控件注册处理器;
4. 在onReceive方法中接收广播。
a) 定义发送广播
b) 配置menifest中接收该action 的receiver 这两个action一致就可以实现接收。
更新appWidget当中的控件状态
在RemoteViews类中有系列方法来对布局参数进行设置
public void onReceive(Context context, Intent intent) { Log.d("mydebug","widget--- onReceive"); String action=intent.getAction(); if(UPDATE_ACTION.equals(action)){ RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.widget); remoteViews.setTextViewText(R.id.widgetText, "test"); AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context); ComponentName componentName=new ComponentName(context, Hb30WidgetProvider.class); appWidgetManager.updateAppWidget(componentName, remoteViews); Log.d("mydebug","I am receiving my news"); }else{ super.onReceive(context, intent); }
<!-- android:configure 如果你想要用户每次添加app widget时候自定义参数如颜色,大小,更新周期或者更多.. 这个时候用 android:configure="com.example.android.ExampleAppWidgetConfigure",它会 让用户自动跳转到该activity,它和一般的activity一样,但是它要能够接收一个包含action="/blog_article/ACTION_APPWIDGET_CONFIGURE/index.html" 的intent --> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget" android:minHeight="72dp" android:minWidth="294dp" android:updatePeriodMillis="86400000" > </appwidget-provider>
如下是为Notification自定义铃声的部分片段
- 铃声选择(已记住上次选择的铃声)
_btnNotificationChooseRington .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); Uri ringtongUri = null; final SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); String uri = sharedPreferences.getString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, "null"); if("null".equals(uri)) { ringtongUri = (Uri) null; } else { ringtongUri = android.net.Uri.parse(uri); } intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.notification_ringtong)); intent.putExtra( RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,ringtongUri); startActivityForResult( intent, RESULT_FIRST_USER); } });
- 铃声保存(注意静音保存为null)
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, String.format("requestCode=%d,resultCode = %d", requestCode, resultCode)); if (resultCode == RESULT_OK ) { Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if (uri != null) { String ringTonePath = uri.toString(); SharedPreferences _prefse = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); _prefse.edit().putString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, ringTonePath).commit(); } else {Log.d(TAG, "null"); SharedPreferences _prefse = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); _prefse.edit().putString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, "null").commit(); } } else if (resultCode == RESULT_CANCELED) { } super.onActivityResult(requestCode, resultCode, data); }
- 铃声设置
private boolean isNotificationSoundEnabled() { //return sharedPrefs.getBoolean(Constants.SETTINGS_SOUND_ENABLED, true); return !sharedPrefs.getString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, "null").equals("null"); } private boolean isNotificationVibrateEnabled() { return sharedPrefs.getBoolean(Constants.SETTINGS_VIBRATE_ENABLED, true); } if(isNotificationSoundEnabled()&&isNotificationVibrateEnabled()){ notification.sound = android.net.Uri.parse(sharedPrefs.getString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, Settings.System.DEFAULT_NOTIFICATION_URI.toString())); long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate; } else if (isNotificationSoundEnabled()&&!isNotificationVibrateEnabled()) { notification.sound = android.net.Uri.parse(sharedPrefs.getString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, Settings.System.DEFAULT_NOTIFICATION_URI.toString())); //notification.defaults = Notification.DEFAULT_SOUND; }
简单的自动页面跳转 附:Menu的使用方法
1,Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/aab" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/welcom_bg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/welcome_bg" android:tag="welcome_ImageView@+id/welcom_bg" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="/blog_article/@drawable/title_30/index.html" /> <ImageView android:id="@+id/welcom_q" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/imageView1" android:layout_centerHorizontal="true" android:layout_marginBottom="31dp" android:src="/blog_article/@drawable/welcom_q/index.html" android:tag="welcome_ImageView@+id/welcom_q" /> </RelativeLayout>
2.WelcomeActivity.java
package com.hb30; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class WelcomeActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); final Intent localIntent=new Intent(this,MainActivity.class); Timer timer=new Timer(); TimerTask tast=new TimerTask() { @Override public void run(){ startActivity(localIntent); } }; timer.schedule(tast,3000); } }
>Menu的使用方法
//当客户点击MENU按钮的时候,调用该方法。
复写public boolean onCreateOptionMenu(Menu menu){
menu.add(0,1,1,R.string.exit);
menu.add(0,2,2,R.string.about);
return super.onCreateOptionsMenu(menu);
}
//当客户点击菜单当中的某一个选项时,会调用该方法
复写onOptionsItemSelected(MenuItem item){
if(item.getItemId()==1){
finish();
}
return super.onOptionsItemSelected(item);
----------------------------------------