android 定时打电话教程

不错的帖子哦

【开源客户端】饭否
http://www.eoeandroid.com/thread-192072-1-1.html

小说分页阅读 支持左右滑屏,添加书签,进度跳转
http://www.eoeandroid.com/thread-192129-1-1.html

移动资讯—— <<我是开发者>> 个人专访
http://www.eoeandroid.com/thread-185404-1-1.html

android 仿苹果日期选择器
http://www.eoeandroid.com/thread-190914-1-1.html

继人员列表,聊天的实现,包括图片,语音,位置,表情
http://www.eoeandroid.com/thread-188604-1-1.html

 

 

由于同学要求,希望有人能定点打电话给她,故开发了这个小程序,如果有人要更多的功能,可以提!

主界面,设置时间并且时间到发送广播
需要了解下PendingIntent ,AlarmManager,Broadcast

package com.zhuyan;

import java.util.Calendar;

import com.zhuyan.broadcast.AlarmReceiver;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimingDialActivity extends Activity {
        /** Called when the activity is first created. */

        private static final String TAG = "TimingDialActivity";
        Button mButtonStart;
        Button mButtonStop;
        EditText telPhoneEditText;

        TextView mTextView;

        Calendar calendar;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);
                calendar = Calendar.getInstance();

                mTextView = (TextView) findViewById(R.id.phone_view);
                mButtonStart = (Button) findViewById(R.id.set_time);
                mButtonStop = (Button) findViewById(R.id.cancel_time);
                telPhoneEditText = (EditText) findViewById(R.id.tel_number);

                mButtonStart.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                calendar.setTimeInMillis(System.currentTimeMillis());
                                int mHour = calendar.get(Calendar.HOUR_OF_DAY);
                                int mMinute = calendar.get(Calendar.MINUTE);
                                new TimePickerDialog(TimingDialActivity.this,
                                                new TimePickerDialog.OnTimeSetListener() {
                                                        public void onTimeSet(TimePicker view,
                                                                        int hourOfDay, int minute) {
                                                                calendar.setTimeInMillis(System
                                                                                .currentTimeMillis());
                                                                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                                                calendar.set(Calendar.MINUTE, minute - 1);
                                                                calendar.set(Calendar.SECOND, 0);
                                                                calendar.set(Calendar.MILLISECOND, 0);

                                                                Intent intent = new Intent(
                                                                                TimingDialActivity.this,
                                                                                AlarmReceiver.class);
                                                                intent.putExtra("tel_phone", telPhoneEditText
                                                                                .getText().toString());
                                                                PendingIntent pendingIntent = PendingIntent
                                                                                .getBroadcast(TimingDialActivity.this,
                                                                                                0, intent, 0);
                                                                AlarmManager am;
                                                                am = (AlarmManager) getSystemService(ALARM_SERVICE);
                                                                // am.set(AlarmManager.RTC_WAKEUP, calendar
                                                                // .getTimeInMillis(), pendingIntent);
                                                                am.setRepeating(AlarmManager.RTC_WAKEUP,
                                                                                calendar.getTimeInMillis(),
                                                                                (24 * 60 * 60 * 1000), pendingIntent);
                                                                SharedPreferences preference = getSharedPreferences("zhuyan",
                                                                                Context.MODE_PRIVATE);
                                                                Editor edit = preference.edit();
                                                                edit.putString("tel_phone", telPhoneEditText.getText().toString());
                                                                edit.commit();
                                                                String tmpS = telPhoneEditText.getText()
                                                                                .toString()
                                                                                + "->"
                                                                                + format(hourOfDay)
                                                                                + ":" + format(minute);
                                                                mTextView.setText(tmpS);
                                                        }
                                                }, mHour, mMinute, true).show();
                        }
                });

                mButtonStop.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                Intent intent = new Intent(TimingDialActivity.this,
                                                AlarmReceiver.class);
                                PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                                TimingDialActivity.this, 0, intent, 0);
                                AlarmManager am;
                                am = (AlarmManager) getSystemService(ALARM_SERVICE);
                                am.cancel(pendingIntent);
                                mTextView.setText("Cancelled");
                        }
                });
        }

        private String format(int x) {
                String s = "" + x;
                if (s.length() == 1)
                        s = "0" + s;
                return s;
        }

        @Override
        protected void onStart() {
                // TODO Auto-generated method stub
                super.onStart();

                SharedPreferences preference = getSharedPreferences("zhuyan",
                                Context.MODE_PRIVATE);
                telPhoneEditText.setText(preference.getString("tel_phone","10086"));
        }

        @Override
        protected void onStop() {
                // TODO Auto-generated method stub
                super.onStop();
                SharedPreferences preference = getSharedPreferences("zhuyan",
                                Context.MODE_PRIVATE);
                Editor edit = preference.edit();
                edit.putString("tel_phone", telPhoneEditText.getText().toString());
                edit.commit();
        }

}

//broadcast  接受广播

package com.zhuyan.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {

        private final static String TAG = "AlarmReceiver";
        private Context context;

        private String telNumber = "10086";

        public void onReceive(Context context, Intent intent) {
                this.context = context;
                SharedPreferences preference =context.getSharedPreferences("zhuyan",
                                Context.MODE_PRIVATE);
                telNumber=preference.getString("tel_phone","10086");
                PhoneCall();
        }

        private void PhoneCall() {
                try {
                        Thread.sleep(2000);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                Uri localUri = Uri.parse("tel:" + telNumber);
                Intent call = new Intent(Intent.ACTION_CALL, localUri);
                call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(call);
                // android.util.Log.v("TeleListener", "start to call");
        }

}

附件Timingforyou2.0
1.增加了第二次呼叫功能呢个,即第二个输入框输入你想要输入的第二与第一次呼叫间隔时间(单位为分),默认值为0表示不进行呼叫;
2.增加了长按第一个编辑框出现所有你的联系人的号码的dialog,目前不支持搜索
  

  

 

 

源码下载

posted on 2012-08-16 11:55  vus520  阅读(5421)  评论(5编辑  收藏  举报

导航