博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 定时打电话教程
阅读量:6197 次
发布时间:2019-06-21

本文共 9073 字,大约阅读时间需要 30 分钟。

不错的帖子哦

【开源客户端】饭否

小说分页阅读 支持左右滑屏,添加书签,进度跳转

移动资讯—— <<我是开发者>> 个人专访

android 仿苹果日期选择器

继人员列表,聊天的实现,包括图片,语音,位置,表情

 

 

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

主界面,设置时间并且时间到发送广播
需要了解下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,目前不支持搜索
  

  

 

 

转载地址:http://pmjca.baihongyu.com/

你可能感兴趣的文章
使用硬盘,安装双系统,Win7+CentOS
查看>>
linux路由管理--专题
查看>>
桌面图标有蓝色阴影
查看>>
nginx coredump调试--
查看>>
分辨率
查看>>
理解Gradle
查看>>
012 制作Java基础镜像
查看>>
Lync2013与Exchange2013集成配置合作伙伴应用程序(二十)
查看>>
回交?
查看>>
判断目录名是否包括某字符
查看>>
JavaScript基础之Date对象与全局属性函数
查看>>
RaidoButton单选框
查看>>
Java 程序员如何快速入门 Android 开发?
查看>>
博为峰Java技术文章 ——JavaSE Swing 按钮
查看>>
flask uwsgi nginx
查看>>
微信小程序的视图容器-- scroll-view
查看>>
RHEL 6.4 安装 Oracle 11.2.0.1 R2
查看>>
UML连接线图例
查看>>
虚拟化--038 vsphere ESXI升级方法
查看>>
C语言指针函数和函数指针
查看>>