以前看过,一段时间就忘了。关键是配置和导出证书,还有开发和产品的证书是分开的,不能合并。 编写push notification之服务器端发送通知
在编写push notification之获取device token中拿到device token以后,需要把token字符串发送给应用的服务器端,即provider。
provider将token号、通知内容、通知形式(比如是否弹出提示窗口、是否发声等)发送给苹果的服务器(apns)。
最简单的provider实现,其实就是通过证书,和苹果服务器建立安全连接(tsl或ssl),通过认证建立连接后,向苹果服务器发送符合苹果要求的数据流。
获得证书苹果提供两种接入方式的证书:
- developer,用于测试
- production,用于产品
如果是内部测试,使用developer方式即可。
下载证书,通过ios provisioning portal:
这要求:
- 登录的apple developer program帐号必须是级别最高的agent(这是针对企业帐号来说的,如果是个人帐号就无所谓了),agent帐号即创始帐号,否则看不到configure链接;
- 必须经过configure操作,已经enable了developer和product。
然后进入configure链接,点击download按钮即可:
处理证书
如果是编写在mac下跑的objc程序,无需对证书做处理,可跳过这一步。
如果是在java下使用,需要把打证书用的私有专用密钥和上述的支持通知的证书(注意,不是iphone developer证书)合并导出。
生成证书:
点击存储的时候,会提示生成一个文件密码:
当然可以密码为空。
之后会提示:
这里需要输入mac登录用户的密码。
文件生成。
编写发送通知的实例
如果是编写mac代码,有一个现成的项目可用:http://stefan.hafeneger.name/download/PushMeBabySource.zip
导入到xcode中,只需将:
deviceToken填写成设备的token字符串,另外,pathForResource改为上面图中的:
aps_developer_identity
另外,要把刚才获得证书步骤中下载的证书复制到xcode项目Resources目录下:
可以看到文件名和上面的pathForResource的参数一致。
之后运行程序就可以在设备上收到推送通知。
如果是用java编写,可以用第三方库,见:
http://code.google.com/p/javapns/
编写简单的发送通知代码:
import org.json.JSONException;
import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
import javapns.data.Device;
import javapns.data.PayLoad;
public class Main {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
PayLoad simplePayLoad = new PayLoad();
// Get PushNotification Instance
PushNotificationManager pushManager = PushNotificationManager.getInstance();
// Link iPhone’s UDID (64-char device token) to a stringName
pushManager.addDevice("iPhone", "00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ");
simplePayLoad.addAlert("My alert message测试");
simplePayLoad.addBadge(1);
simplePayLoad.addSound("default");
Device client = PushNotificationManager.getInstance().getDevice("iPhone");
PushNotificationManager.getInstance().initializeConnection("gateway.sandbox.push.apple.com", 2195, "/home/ubuntu/mypush.p12", "password", SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
PushNotificationManager.getInstance().sendNotification(client, simplePayLoad);
测试中文没有乱码问题。
编写比较复杂的使用示例(可以控制通知是否有提示窗口、是否有提醒声音):
- aPayload.addBadge( 2),显示在手机应用图标上的数字
- aPayload.addAlert("软件版本有更新"),显示提示窗口文字
- aPayload.addSound("default.wav"),指定提示声音
另外,也可以使用php的第三方实现,比如:
http://code.google.com/p/php-apns
基本原理是启动一个php服务,监控memcacheq队列,如果有消息就发送给苹果服务器。
要实现添加按钮的操作,必须自定义Adapter,使用Button View的setTag()方法,将Button所属的位置设置到tag当中
要实现动态添加删除ItemView的操作,必须首先调整调整Adapter所绑定的数据源,然后调用Adapter的notifyDataSetChanged()方法
以下为实现的一个实例
package com.jason.joysmsyd; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class SendMain extends ListActivity implements OnClickListener{ Button buttonMessage,buttonContact,buttonHistory; EditText textMessage; List<Map<String,String>> contacts = new ArrayList<Map<String,String>>(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.setContentView(R.layout.layout_send); buttonMessage = (Button) this.findViewById(R.id.ButtonMessage); buttonContact = (Button) this.findViewById(R.id.ButtonContact); buttonHistory = (Button) this.findViewById(R.id.ButtonHistory); textMessage = (EditText)this.findViewById(R.id.EditTextMessage); textMessage.setText(this.getIntent().getExtras().getString("message")); } public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.ButtonMessage: this.finish(); break; case R.id.ButtonContact: { Intent intent = new Intent(); intent.setAction("com.jason.action.contact"); this.startActivityForResult(intent, 0); } break; case R.id.ButtonHistory: { Intent intent = new Intent(); intent.setAction("com.jason.action.history"); this.startActivityForResult(intent, 1); } break; } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0 && resultCode == RESULT_OK) { this.getcontactFromString(data.getExtras().getString( UserSelectActivity.RETURN_LIST)); bindDataToList(); } } private void getcontactFromString(String data) { if (data == null || data.length() == 0) { return; } String[] arrayContact = data.split("#"); for (String singleContact : arrayContact) { if (singleContact != null && singleContact.length() > 0) { String[] props = singleContact.split(":"); if (props.length == 2) { Map<String,String> contact = new HashMap<String,String>(); contact.put("name", props[0]); contact.put("phone", props[1]); contacts.add(contact); } } } } private void bindDataToList(){ this.setListAdapter(new MyAdapter()); } public class MyAdapter extends BaseAdapter{ public int getCount() { // TODO Auto-generated method stub return contacts.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return contacts.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater inflater = SendMain.this.getLayoutInflater(); final View view = inflater.inflate(R.layout.layout_user_item, null); final TextView textPhone = (TextView) view.findViewById(R.id.text1); final TextView textName = (TextView) view.findViewById(R.id.text2); Button button = (Button)view.findViewById(R.id.buttonDelete); textPhone.setText(contacts.get(position).get("phone")); textName.setText(contacts.get(position).get("name")); button.setTag( position); button.setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub int position = Integer.parseInt(v.getTag().toString()); contacts.remove(position); MyAdapter.this.notifyDataSetChanged(); // SendMain.this.getListView().refreshDrawableState(); }}); return view; } } }
转自:http://www.cnblogs.com/qzxia/archive/2010/11/29/1890959.html
Android系统自带的是星星,我们不能随心所欲的更换自己喜欢的图片,下面我就来讲讲如何制作自己的RatingBar样式。
1、在res/data/style.xml文件中定义种样式:
<resources> <style name="myratingbar" parent="@android:style/Widget.RatingBar.Small"> <item name="android:progressDrawable"> @drawable/myratingbar </item> <item name="android:minHeight">36dip</item> <item name="android:maxHeight">36dip</item> <style> </resources>
其中继承的父类可以选择RatingBar,RatingBar.Indicator,RatingBar.Small;
大小可以自己定义。
注意:RatingBar,RatingBar.Indicator,RatingBar.Small的不同点除了他们的显示大小不同以外,主要的还有Ratingbar中的IsIndicator属性是false,其他两种是true.该属性为false表示可以相应用的点击事件,通过这种方式可以和用户交互。
2、在res/drawable/myratingbar.xml文件指定不同图层的图片资源
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+android:id/background" android:drawable="@drawable/myratingbar_off" /> <item android:id="@+android:id/secondaryProgress" android:drawable="@drawable/myratingbar_half" /> <item android:id="@+android:id/progress" android:drawable="@drawable/myratingbar_on" /> </layer-list>
android评分条RatingBar自定义设置
http://fariytale.iteye.com/blog/1260673
RatingBar自定义设置
http://wang-peng1.iteye.com/blog/720956
SeekBar自定义
http://heji.iteye.com/blog/669266
Android下修改SeekBar样式
http://blog.csdn.net/vrix/archive/2010/08/03/5785676.aspx
垂直显示的SeekBar
import android.content.Context; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.AbsSeekBar; public class VerticalSeekBar extends AbsSeekBar { private Drawable mThumb; private int height; private int width; public interface OnSeekBarChangeListener { void onProgressChanged(VerticalSeekBar VerticalSeekBar, int progress, boolean fromUser); void onStartTrackingTouch(VerticalSeekBar VerticalSeekBar); void onStopTrackingTouch(VerticalSeekBar VerticalSeekBar); } private OnSeekBarChangeListener mOnSeekBarChangeListener; public VerticalSeekBar(Context context) { this(context, null); } public VerticalSeekBar(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.seekBarStyle); } public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) { mOnSeekBarChangeListener = l; } void onStartTrackingTouch() { if (mOnSeekBarChangeListener != null) { mOnSeekBarChangeListener.onStartTrackingTouch(this); } } void onStopTrackingTouch() { if (mOnSeekBarChangeListener != null) { mOnSeekBarChangeListener.onStopTrackingTouch(this); } } void onProgressRefresh(float scale, boolean fromUser) { Drawable thumb = mThumb; if (thumb != null) { setThumbPos(getHeight(), thumb, scale, Integer.MIN_VALUE); invalidate(); } if (mOnSeekBarChangeListener != null) { mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser); } } private void setThumbPos(int w, Drawable thumb, float scale, int gap) { int available = w+getPaddingLeft()-getPaddingRight(); int thumbWidth = thumb.getIntrinsicWidth(); int thumbHeight = thumb.getIntrinsicHeight(); available -= thumbWidth; // The extra space for the thumb to move on the track available += getThumbOffset() * 2; int thumbPos = (int) (scale * available); int topBound, bottomBound; if (gap == Integer.MIN_VALUE) { Rect oldBounds = thumb.getBounds(); topBound = oldBounds.top; bottomBound = oldBounds.bottom; } else { topBound = gap; bottomBound = gap + thumbHeight; } thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound); } protected void onDraw(Canvas c) { c.rotate(-90); c.translate(-height,0); super.onDraw(c); } protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { width = 22; height = 160; this.setMeasuredDimension(width, height); } @Override public void setThumb(Drawable thumb) { mThumb = thumb; super.setThumb(thumb); } protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(h, w, oldw, oldh); } public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: setPressed(true); onStartTrackingTouch(); trackTouchEvent(event); break; case MotionEvent.ACTION_MOVE: trackTouchEvent(event); attemptClaimDrag(); break; case MotionEvent.ACTION_UP: trackTouchEvent(event); onStopTrackingTouch(); setPressed(false); break; case MotionEvent.ACTION_CANCEL: onStopTrackingTouch(); setPressed(false); break; } return true; } private void trackTouchEvent(MotionEvent event) { final int Height = getHeight(); final int available = Height - getPaddingBottom() - getPaddingTop(); int Y = (int)event.getY(); float scale; float progress = 0; if (Y > Height - getPaddingBottom()) { scale = 0.0f; } else if (Y < getPaddingTop()) { scale = 1.0f; } else { scale = (float)(Height - getPaddingBottom()-Y) / (float)available; } final int max = getMax(); progress = scale * max; setProgress((int) progress); } private void attemptClaimDrag() { if (getParent() != null) { getParent().requestDisallowInterceptTouchEvent(true); } } public boolean dispatchKeyEvent(KeyEvent event) { if(event.getAction()==KeyEvent.ACTION_DOWN) { KeyEvent newEvent = null; switch(event.getKeyCode()) { case KeyEvent.KEYCODE_DPAD_UP: newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_RIGHT); break; case KeyEvent.KEYCODE_DPAD_DOWN: newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_LEFT); break; case KeyEvent.KEYCODE_DPAD_LEFT: newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_DOWN); break; case KeyEvent.KEYCODE_DPAD_RIGHT: newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_UP); break; default: newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,event.getKeyCode()); break; } return newEvent.dispatch(this); } return false; } }
关于SeekBar无法拖到最大值的解决办法:
seekbar的android:layout_width设置为fill_parent。
假如seekbar的max值比较小的话,那无法拖到最后的最大值。
解决方法很简单,加padding:
android:paddingLeft="13dip"
android:paddingRight="13dip"