首页 技术 正文
技术 2022年11月12日
0 收藏 424 点赞 3,802 浏览 2813 个字

利用广播实现ip拨号

布局文件:

<?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"
> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入ip号码前缀"
/> <EditText
android:id="@+id/phoneNum"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <Button
android:text="保存"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

activity:

package com.heyiyong.ipdial;import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;public class MyActivity extends Activity {
EditText editText; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); this.editText = (EditText) findViewById(R.id.phoneNum); SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
String number = sharedPreferences.getString("number", "");
editText.setText(number);
} public void click(View view) {
SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("number", editText.getText().toString());
editor.commit();
}
}

接收者:

package com.heyiyong.ipdial;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;/**
* Created by Administrator on 14-1-2.
*/
public class DialReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String currentNumber = getResultData();
//获取设置的ip号前缀
SharedPreferences sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
String ipNumber = sharedPreferences.getString("number","");
String newNumber = ipNumber+currentNumber;
setResultData(newNumber);
}
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.heyiyong.ipdial"
android:versionCode="1"
android:versionName="1.0"> <uses-sdk android:minSdkVersion="18"/> <!--去电号码获取-->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <application
android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity
android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity> <!-- 去电广播接收者 -->
<receiver android:name=".DialReceiver">
<intent-filter>
<!--监听去电-->
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver> </application>
</manifest>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289