当前位置: 代码迷 >> Iphone >> 仿照iphone弹出式短信查看
  详细解决方案

仿照iphone弹出式短信查看

热度:58   发布时间:2016-04-25 06:41:25.0
模仿iphone弹出式短信查看
感觉使用抽屉查看短信麻烦,于是自己写了一个模仿iphone查看的短信的小程序,自己用,挺好的。实现原理主要是activity的背景半透明,加上收到短信开启服务和桌面图标开启服务。目前只做了收到1条短信显示,如果是长短信和连续短信的话没有做处理,废话少说,附件是源码和截图。
popSMS.java 闪屏,首次开启应用显示
package com.smsshow;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;public class popSMS extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {	// TODO Auto-generated method stub	super.onCreate(savedInstanceState);	setContentView(R.layout.start_page);		new Handler().postDelayed(new Runnable() {		public void run() {			startService(new Intent(popSMS.this, MyService.class));			popSMS.this.finish();		}	}, 2000);}}

注:此Receiver无用,但是贴上提醒一下童鞋们
BootBroadcastReceiver.java 本来的想法是开机开启服务,但是某些rom比如htc,lenovo是不公开BOOT_COMPLETED广播的,我们自己的应用接收不到,所以我使用了收到短信开启服务
package com.smsshow;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BootBroadcastReceiver extends BroadcastReceiver {	@Override	public void onReceive(Context context, Intent intent) {		// TODO Auto-generated method stub		//if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){			Log.i("tag","get boot completed action");			Intent serviceIntent = new Intent();			serviceIntent.setClass(context, MyService.class);			context.startService(serviceIntent);		//}	}}


MyService.java 接收短信服务
package com.smsshow;import android.app.Service;import android.content.BroadcastReceiver;import android.content.ContentResolver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.database.Cursor;import android.net.Uri;import android.os.AsyncTask;import android.os.Bundle;import android.os.IBinder;import android.telephony.SmsMessage;import android.util.Log;public class MyService extends Service {	BroadcastReceiver mReceiver;	@Override	public int onStartCommand(Intent intent, int flags, int startId) {		// TODO Auto-generated method stub		mReceiver = new SMSReceive();		IntentFilter filter = new IntentFilter();		filter.addAction("android.provider.Telephony.SMS_RECEIVED");		registerReceiver(mReceiver, filter);		return super.onStartCommand(intent, flags, startId);	}	@Override	public IBinder onBind(Intent intent) {		// TODO Auto-generated method stub		Log.i("tag", "my sms service is on!");		return null;	}	private void showMsg(String title, String message) {		Intent startIntent = new Intent();		startIntent.setClass(this, SmsShowActivity.class);		startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);		startIntent.putExtra("title", title);		startIntent.putExtra("message", message);		startActivity(startIntent);	}	class InnerTask extends AsyncTask<String, Object, Object> {		String contectId;		String msgNumber;		String msgBody;		@Override		protected Object doInBackground(String... params) {			// TODO Auto-generated method stub			msgNumber = params[0];			msgBody = params[1];			contectId = getContactIDFromPhoneNum(msgNumber);			return null;		}		@Override		protected void onPostExecute(Object result) {			// TODO Auto-generated method stub			if (contectId == null) {				showMsg(msgNumber, msgBody);			} else {				showMsg(contectId, msgBody);			}			super.onPostExecute(result);		}		public String getContactIDFromPhoneNum(String phoneNum) {			String contactName = null;			ContentResolver resolver = MyService.this.getContentResolver();			Uri uri = Uri					.parse("content://com.android.contacts/data/phones/filter/"							+ phoneNum);			Cursor c = resolver.query(uri, new String[] { "display_name" },					null, null, null);			while (c.moveToNext()) {				contactName = c.getString(0);			}			Log.i("tag", "contactname" + contactName);			return contactName;		}	}	@Override	public void onDestroy() {		// TODO Auto-generated method stub		unregisterReceiver(mReceiver);		super.onDestroy();	}	class SMSReceive extends BroadcastReceiver {		static final String TAG = "SMSReceive";		static final String smsuri = "android.provider.Telephony.SMS_RECEIVED";		@Override		public void onReceive(Context arg0, Intent arg1) {			if (arg1.getAction().equals(smsuri)) {				Bundle bundle = arg1.getExtras();				if (null != bundle) {					Object[] pdus = (Object[]) bundle.get("pdus");					SmsMessage[] smg = new SmsMessage[pdus.length];					for (int i = 0; i < pdus.length; i++) {						smg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);						Log.i(TAG + "smg" + i, smg[i].toString());					}					for (SmsMessage cursmg : smg) {						String msgBody = cursmg.getMessageBody();						String msgNumber = cursmg.getOriginatingAddress();						new InnerTask().execute(msgNumber, msgBody);					}					// abortBroadcast(); //终止此条广播				}			}		}	}}


SmsShowActivity.java Activity显示
package com.smsshow;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class SmsShowActivity extends Activity {	/** Called when the activity is first created. */		String title;	String message;	Button btn;	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		init();	}		private void init(){		title = getIntent().getStringExtra("title");		message = getIntent().getStringExtra("message");				bindData();	}	private void bindData(){		TextView tv_title = (TextView)findViewById(R.id.tv_title);		tv_title.setText("From: " + title);		TextView tv_message = (TextView)findViewById(R.id.tv_message);		tv_message.setText(message);		btn = (Button) findViewById(R.id.btn);		btn.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub					SmsShowActivity.this.finish();			}		});	}			}


Manifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest	xmlns:android="http://schemas.android.com/apk/res/android"	package="com.smsshow"	android:versionCode="1"	android:versionName="1.0">	<uses-sdk android:minSdkVersion="8" />	<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>	<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>	<application		android:icon="@drawable/icon"		android:label="@string/app_name">		<activity			android:name=".popSMS"			android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"			android:screenOrientation="portrait">			<intent-filter>				<action android:name="android.intent.action.MAIN" />				<category android:name="android.intent.category.LAUNCHER" />			</intent-filter>		</activity>		<receiver android:name="com.smsshow.BootBroadcastReceiver">			<intent-filter android:priority="1000">				<action android:name="android.provider.Telephony.SMS_RECEIVED" />			</intent-filter>		</receiver>		<activity			android:name=".SmsShowActivity"			android:theme="@android:style/Theme.Translucent.NoTitleBar"			android:screenOrientation="portrait" />		<service android:name=".MyService" />	</application></manifest>
  相关解决方案