http://apps.hi.baidu.com/share/detail/43342917
看到同事都用來“自動重播”軟件來訂火車票了,我也下載一個來看看,下載回來反編譯一下看看,好像有廣告的啊,有一個好像是Google的還是那個廣告類在里面。索性自己弄一個好了,現在吸金軟件很多啊,自己編譯的才放心,呵呵!代碼不多,又有反編譯過來的代碼作為才看,雖然反編譯過來好像有的小地方不是很對,不過代碼邏輯還大概看得出來的,然后自己看了下Android SDK的文檔,Google了兩下,大概可以弄個出來。就是那個鬼模擬器和Eclipse太好資源了。在我的機器上幾乎跑不動了,浪費點時間。當然看Android SDK文檔理解一下基本概念也花了不少時間。
不過很悲劇啊,早上起來快9點了,然后我用軟件一撥進去了,結果我搞錯時間了,以為還沒到我要買的日期就退了出來。等過一會再撥的時候票已經賣完了,有同事說是分時間段放票出來的,再試試看或者有"軟a臥"也買了。
來看程序吧:
整個程序的代碼邏輯很簡單,就是先啟動一個activity界面給用戶輸入,然后啟動一個后臺service不停的自動撥打相應的號碼,直到撥通為止。Android下面相應的關鍵功能實現代碼:
1. 撥打電話
通過Intent.ACTION_CALL 來請求系統的撥號Activity就可以了。在Android系統里面一個 Intent其實就相當于一條消息,然后系統就會自動根據的你的請求去找相應的功能模塊比如說package里面的那個類來加載了。應用都事先注冊了說我自己能夠接受那些Intent的。撥打電話也有他的一個Intent 就是 Intent.ACTION_CALL ,其他的發送短信啊那些也有其他的,自己看下文檔就知道了。
private void PhoneCall()
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mJustCall = false;
Uri localUri = Uri.parse("tel:" + mPhoneNumber);
Intent call = new Intent(Intent.ACTION_CALL, localUri);
call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);;
startActivity(call);
//android.util.Log.v("TeleListener", "start to call");
mDialedCount ++;
}
注意這個功能要android.permission.CALL_PHONE的權限才能使用,所以要在AndroidManifest.xml 文件后面加上這句
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
安裝程序的時候,有什么“需要收費的服務什么的”提示就是這條了。
2. 手機通話狀態的查詢
這個程序還有一個功能就是能夠判斷手機是不是正在撥號啊通話啊,才好控制自動重播的。這個Android有通知事件過來的,就像以前的“位置感應"事件一樣,只要你注冊了這個通知事件,手機狀態變化的時候就自動調用你Listener了。不過Android手機這個事件沒法區分“撥號”和“通話”這兩個狀態的,這兩者統一都是TelephonyManager.CALL_STATE_OFFHOOK狀態,TelephonyManager.CALL_STATE_RINGING指的是外面撥進來的響鈴吧。不過你看他系統里面其實是可以區分“撥號”和“通話”這兩個狀態的,打通了他就開始計時,界面也顯示“正在通話”,只是它沒有向外部應用開放這個接口而已。可以去看Android源碼的packages/apps/Phone/src/com/android/phone/ 下面的InCallScreen.java CallCard.java 等文件。鑒于我們的“xxxxx”,可以到http://www.netmite.com/android/mydroid/packages/apps/Phone/src/com/android/phone/ 這里去看吧,網上好像有很多提供在線源碼的
http://hi-android.info/src/com/android/phone/
http://gitorious.org/0xdroid/packages_apps_phone/trees/5f6f01ecda4336dfb47108e67ff909a65f14b820/src/com/android/phone
另外監a聽這個事件需要權限<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
其實網上那些軟件比如說“別人大個電話過來播放隨機鈴聲”應該也是通過這個來做到的吧。
TelephonyManager telephonyMgr = (TelephonyManager)getSystemService("phone");
TeleListener teleListener = new TeleListener(this);
telephonyMgr.listen(teleListener, 0);
class TeleListener extends PhoneStateListener {
private AutoRedialService manager;
public TeleListener(AutoRedialService a)
{
this.manager = a;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
// 當處于待機狀態中
case TelephonyManager.CALL_STATE_IDLE: {
manager.Log("IDLE");
//android.util.Log.v("TeleListener", "IDLE");
if (manager.ShouldStop() || manager.LastCallSucceed()) {
manager.stopSelf();
break;
}
PhoneCall();
break;
}
// 當處于正在撥號出去,或者正在通話中
case TelephonyManager.CALL_STATE_OFFHOOK: {
manager.Log("OFFHOOK");
//android.util.Log.v("TeleListener", "OFFHOOK");
mJustCall = true;
//Timer t = new Timer();
break;
}
// 外面撥進來,好沒有接撥號狀態中..
case TelephonyManager.CALL_STATE_RINGING: {
manager.Log("RINGING");
//android.util.Log.v("TeleListener", "RINGING");
break;
}
default:
break;
}
}
3. "通一話一記一錄"查詢 (媽的這都是關鍵詞啊,搞我半天發不上了)
上面說了不能區分"接通"與“撥號”狀態,所以我想到的辦法只有撥完之后再去查看最近的通話記錄了,如果最后一條記錄的通話時間是大于0的,說明就撥通了,就不需要再重播了。這個用起來還可以,程序撥通就給我退出了。在Android里面這個信息需要向“content provider內a容提供者”去要,其實就是類似查詢數據庫,可以查、增、減之類的,自己的程序也可以提供這種接口供別人查詢。注意這個也需要權a限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
我一開始不知道,結果用那個蝸牛一樣模擬器調試了半天才調試出來。
private boolean LastCallSucceed ()
{
if ( mJustCall == false) {
return false;
}
String[] projection = new String[] {
Calls.NUMBER,
Calls.DURATION
};
ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(android.provider.CallLog.Calls.CONTENT_URI,
projection,
null,
null,
Calls.DEFAULT_SORT_ORDER);
if (cur.moveToFirst()) {
int duration = cur.getInt(1);
//上次通話時間
if (duration > 0 )
{
//android.util.Log.v("TeleListener", "|"+ String.valueOf(duration) + "|");
//Log( "|"+ String.valueOf(duration) + "|");
return true;
}
}
return false;
}
4. Activity 和Service直接的通訊
在Android里面activity和service是分別屬于兩個不同的進程的,要兩者交互還挺麻煩的,按照文檔說法,要實現bindservice等大堆接口,弄起來挺麻煩的,我這一個這么小玩意就算了吧。偷看了一下反編譯的出來的代碼,里面啟動service的時候,activity發送出去Intent消息攜帶多一點信息過去,然后在service里面可以讀出來,但是service想傳點數據回activity就不行了。這種簡單的辦法也夠用了,還有朋友發現什么簡單辦法可以告訴我一下。
在 activity中
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button_Start:
Intent msg = new Intent(this, AutoRedialService.class);
EditText pnumEdit = (EditText) findViewById(R.id.EditText_phoneNumber);
EditText retryEdit = (EditText)findViewById(R.id.EditText_RetryCount);
String phoneNumber = pnumEdit.getText().toString();
Integer i = Integer.decode(retryEdit.getText().toString());
int retryCount = i.intValue();
msg.putExtra("RetryCount", retryCount);
msg.putExtra("PhoneNumber",phoneNumber);
//retryEdit.setText(String.valueOf(retryCount-1));
startService(msg);
然后在service啟動的時候可以讀出來
public void onStart(Intent intent, int startID) {
super.onStart(intent, startID);
mRetryCount = intent.getIntExtra("RetryCount", 0);
String tmp = intent.getStringExtra("PhoneNumber");
if (tmp != null)
{
mPhoneNumber = tmp;
}
寫完之后,我發程序發到我的手機上去試了一下,好像還行啊,有時需要撥幾十次才能打通電話,呵呵,真是省去不少功夫了。有點小問題,有時想停止的時候,不那么好控制,我把重撥間隔定為2秒了,要操作的很快才行啊。還有就是那個“撥號”“通話”兩種狀態的變化,感覺可以去讀取撥號程序的界面來判斷,如果能夠找到那個activity的實例,然后就好辦了再findViewById就可以讀出上面控件的狀態了。不過android好像很難獲取別的activity的實例,即使這個應用是自己的同一個application啟動起來的。看文檔好像通過android 測試接口Instrumentation 下面的的activitymonitor什么也許可以獲取的 到其他activity的實例,不過我沒有去試,這個需要建一個“android測試”項目然后從那開始吧。
完整的代碼
================AutoRedialService。java================================
package widebright.AutoRedial;
import java.util.Timer;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.CallLog.Calls;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class AutoRedialService extends Service {
private int mRetryCount = 0;
private int mDialedCount = 0;
private String mPhoneNumber= "10086";
private String mDebugLog = "";
private boolean mJustCall = false;
public void Log (String text){
//mDebugLog += text;
}
@Override
public IBinder onBind(Intent msg) {
// 使用 bind的辦法,可以方便的在service和
//activity兩個不同的進程直接交互,不過看代碼很多啊
return null;
}
private void PhoneCall()
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mJustCall = false;
Uri localUri = Uri.parse("tel:" + mPhoneNumber);
Intent call = new Intent(Intent.ACTION_CALL, localUri);
call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);;
startActivity(call);
//android.util.Log.v("TeleListener", "start to call");
mDialedCount ++;
}
private boolean ShouldStop()
{
return mDialedCount > mRetryCount;
}
private boolean LastCallSucceed ()
{
if ( mJustCall == false) {
return false;
}
String[] projection = new String[] {
Calls.NUMBER,
Calls.DURATION
};
ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(android.provider.CallLog.Calls.CONTENT_URI,
projection,
null,
null,
Calls.DEFAULT_SORT_ORDER);
if (cur.moveToFirst()) {
int duration = cur.getInt(1);
//上次通話時間
if (duration > 0 )
{
//android.util.Log.v("TeleListener", "|"+ String.valueOf(duration) + "|");
//Log( "|"+ String.valueOf(duration) + "|");
return true;
}
}
return false;
}
public void onCreate()
{
super.onCreate();
}
public void onStart(Intent intent, int startID) {
super.onStart(intent, startID);
//android.util.Log.v("TeleListener", "starting haha");
// 獲取電話管理的一個類實例
mRetryCount = intent.getIntExtra("RetryCount", 0);
String tmp = intent.getStringExtra("PhoneNumber");
if (tmp != null)
{
mPhoneNumber = tmp;
}
TelephonyManager telephonyMgr = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
// 建立一個監聽器來實時監聽電話的通話狀態
telephonyMgr.listen(new TeleListener(this),
PhoneStateListener.LISTEN_CALL_STATE);
mDialedCount = 0;
//PhoneCall();
}
public void onDestroy()
{
TelephonyManager telephonyMgr = (TelephonyManager)getSystemService("phone");
TeleListener teleListener = new TeleListener(this);
telephonyMgr.listen(teleListener, 0);
mDialedCount = 0;
mRetryCount = 0;
mPhoneNumber= "10086";
super.onDestroy();
}
class TeleListener extends PhoneStateListener {
private AutoRedialService manager;
public TeleListener(AutoRedialService a)
{
this.manager = a;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
// 當處于待機狀態中
case TelephonyManager.CALL_STATE_IDLE: {
manager.Log("IDLE");
//android.util.Log.v("TeleListener", "IDLE");
if (manager.ShouldStop() || manager.LastCallSucceed()) {
manager.stopSelf();
break;
}
PhoneCall();
break;
}
// 當處于正在撥號出去,或者正在通話中
case TelephonyManager.CALL_STATE_OFFHOOK: {
manager.Log("OFFHOOK");
//android.util.Log.v("TeleListener", "OFFHOOK");
mJustCall = true;
//Timer t = new Timer();
break;
}
// 外面撥進來,好沒有接撥號狀態中..
case TelephonyManager.CALL_STATE_RINGING: {
manager.Log("RINGING");
//android.util.Log.v("TeleListener", "RINGING");
break;
}
default:
break;
}
}
}
}
===============================main.java================================
package widebright.AutoRedial;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class main extends Activity implements OnClickListener {
Button buttonStart, buttonStop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//EditText pnumEdit = (EditText) findViewById(R.id.EditText_phoneNumber);
//EditText retryEdit = (EditText)findViewById(R.id.EditText_RetryCount);
//retryEdit.setText("10");
buttonStart = (Button) findViewById(R.id.Button_Start);
buttonStop = (Button) findViewById(R.id.Button_Stop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button_Start:
Intent msg = new Intent(this, AutoRedialService.class);
EditText pnumEdit = (EditText) findViewById(R.id.EditText_phoneNumber);
EditText retryEdit = (EditText)findViewById(R.id.EditText_RetryCount);
String phoneNumber = pnumEdit.getText().toString();
Integer i = Integer.decode(retryEdit.getText().toString());
int retryCount = i.intValue();
msg.putExtra("RetryCount", retryCount);
msg.putExtra("PhoneNumber",phoneNumber);
//retryEdit.setText(String.valueOf(retryCount-1));
startService(msg);
break;
case R.id.Button_Stop:
stopService(new Intent(this, AutoRedialService.class));
break;
default:
break;
}
}
}
=====================AutoRedialManifest.xm===========================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="widebright.AutoRedial"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".main" android:icon="@drawable/icon"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:icon="@drawable/icon" android:label="@string/app_name"
android:name=".AutoRedialService">
</service>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
</manifest>
============================layout/main.xml=============================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_gravity="right">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView android:text="@string/number"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="fill_parent"
android:singleLine="true"
android:layout_height="wrap_content"
android:text="10086"
android:phoneNumber="true"
android:id="@+id/EditText_phoneNumber">
</EditText>
<TextView android:text="@string/times"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="100"
android:id="@+id/EditText_RetryCount"
android:numeric="integer"
android:singleLine="true"
android:inputType="number">
</EditText>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:text="@string/start"
android:id="@+id/Button_Start"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="left">
</Button>
<Button android:text="@string/stop"
android:id="@+id/Button_Stop"
android:layout_height="wrap_content" android:layout_gravity="right" android:layout_width="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
==========================================
再發兩個網上找到的小實例,沒有測試過
1. 模擬鍵盤按鍵
final IWindowManager windowManager = IWindowManager.Stub
.asInterface(ServiceManager.getService("window"));
windowManager.injectchangerchangerKeyEvent(kEvent,true);
2. 攔截撥打電話號碼操作
1.第一步,寫一個Receiver繼承自BroadcastReceiver
public class PhoneStatReceiver extends BroadcastReceiver{
private static final String TAG = "PhoneStatReceiver";
// private static MyPhoneStateListener phoneListener = new MyPhoneStateListener();
private static boolean incomingFlag = false;
private static String incoming_number = null;
@Override
public void onReceive(Context context, Intent intent) {
//如果是撥打電話
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
incomingFlag = false;
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "call OUT:"+phoneNumber);
}else{
//如果是來電
TelephonyManager tm =
(TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
incomingFlag = true;//標識當前是來電
incoming_number = intent.getStringExtra("incoming_number");
Log.i(TAG, "RINGING :"+ incoming_number);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(incomingFlag){
Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if(incomingFlag){
Log.i(TAG, "incoming IDLE");
}
break;
}
}
}
}
第二步:在AndroidManifest.xml,配置寫好的Receiver,并攔截相應的BroadCastAction,
另外注意加上相應的權限。
<receiver android:name=".filter.PhoneStatReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
另外有撥打緊急號碼權限
<uses-permission android:name="android.permission.CALL_PRIVILEGED" />