Java 如何在GPRS spinner中添加Wi-Fi选项
在我的应用程序中,我使用“apn”进行GPRS连接显示。我编写了一个微调器,在其中我可以获得Telenor GPRS、Telenor MMS和Telenor WAP。 **我想在此微调器中添加Wi-Fi选项。当我选择Wi-Fi选项时,设备开始感应Wi-Fi 问:如何在微调器中添加Wi-Fi选项?? ** 这是我的密码Java 如何在GPRS spinner中添加Wi-Fi选项,java,android,android-layout,android-intent,Java,Android,Android Layout,Android Intent,在我的应用程序中,我使用“apn”进行GPRS连接显示。我编写了一个微调器,在其中我可以获得Telenor GPRS、Telenor MMS和Telenor WAP。 **我想在此微调器中添加Wi-Fi选项。当我选择Wi-Fi选项时,设备开始感应Wi-Fi 问:如何在微调器中添加Wi-Fi选项?? ** 这是我的密码 Spinner GPRS; String [] name_of_GPRS__available; int [] apn_id; public void onCrea
Spinner GPRS;
String [] name_of_GPRS__available;
int [] apn_id; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration);
EnumerateAPNs();
/* this is a android enviroment in which you can develop an android application in which you
* share all your basic necessities of thrkife bghhr4y2ghrrr*/
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GPRS = (Spinner)findViewById(R.id.GPRS);
ArrayAdapter<?> spinner_array = new ArrayAdapter<Object>(this,android.R.layout.simple_dropdown_item_1line,name_of_GPRS__available);
spinner_array.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Editor prefsEditor = prefs.edit();
prefsEditor.putString("Object", name_of_GPRS__available.toString());
prefsEditor.commit();
GPRS.setAdapter(spinner_array);
//GPRS.setOnItemSelectedListener(MyOnItemSelectedListener());
GPRS.setOnItemSelectedListener(new MyOnItemSelectedListener());`
GPRS.setAdapter(spinner_array);
//GPRS.setOnItemSelectedListener(MyOnItemSelectedListener());
GPRS.setOnItemSelectedListener(new MyOnItemSelectedListener());
public void onItemSelected(AdapterView<?> parent, View view,
final int position, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(position)
SetDefaultAPN(apn_id[position]);
Toast.makeText(parent.getContext(), "ETracking System Selects " +
parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
public boolean SetDefaultAPN(int id)
{
boolean res = false;
ContentResolver resolver = Configuration.this.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", id);
try
{
resolver.update(Uri.parse("content://telephony/carriers/preferapn"), values, null, null);
Cursor c = resolver.query(
Uri.parse("content://telephony/carriers/preferapn"),
null,
"_id="+id,
null,
null);
if(c != null)
{
res = true;
c.close();
}
}
catch (SQLException e)
{
//Log.d("TAG", e.getMessage());
}
return res;
}
/*
* Enumerate all APN data
*/
private void EnumerateAPNs()
{
Cursor c = this.getContentResolver().query(
Uri.parse("content://telephony/carriers/current"), null, null, null, null);
if (c != null)
{
//String s = "All APNs:\n";
//Log.d("TAG", s);
try
{
printAllData(c); //Print the entire result set
}
catch(SQLException e)
{
Toast.makeText(Configuration.this, "No Network Connection Available", Toast.LENGTH_LONG).show();
}
c.close();
}
}
/*
* Print all data records associated with Cursor c.
* Return a string that contains all record data.
* For some weird reason, Android SDK Log class cannot print very long string message.
* Thus we have to log record-by-record.
*/
private void printAllData(Cursor c)
{
//if(c == null) return null;
if(c.moveToFirst())
{
name_of_GPRS__available = new String[c.getCount()];
apn_id = new int [c.getCount()];
int i= 0;
do{
name_of_GPRS__available [i]= c.getString(c.getColumnIndex("name"));
apn_id[i]=c.getInt(c.getColumnIndex("_id"));
//Log.d("TAG",name[i]);
i++;
}while(c.moveToNext());
//Log.d("TAG","End Of Records");
//name_of_GPRS_available [1]=" GPRS";
}
}
Spinner-GPRS;
字符串[]可用的\u GPRS\u的名称\u;
int[]apn_id;创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration);
枚举apns();
/*这是一个android环境,您可以在其中开发一个android应用程序
*分享您的所有基本必需品thrkife BGHHR4Y2GHRR*/
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入\状态\始终\隐藏);
tm=(TelephonyManager)getSystemService(Context.TELEPHONY_服务);
GPRS=(微调器)findviewbyd(R.id.GPRS);
ArrayAdapter spinner\u array=新的ArrayAdapter(这是android.R.layout.simple\u下拉列表\u项目\u 1行,可用的\u GPRS\u名称);
spinner\u array.setDropDownViewResource(android.R.layout.simple\u dropdown\u item\u 1line);
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(this.getApplicationContext());
编辑器prefsEditor=prefs.edit();
prefsEditor.putString(“Object”,名称为\u GPRS\u可用。toString());
提交();
GPRS.setAdapter(微调器阵列);
//GPRS.setOnItemSelectedListener(MyOnItemSelectedListener());
GPRS.setOnItemSelectedListener(新的MyOnItemSelectedListener())`
GPRS.setAdapter(微调器阵列);
//GPRS.setOnItemSelectedListener(MyOnItemSelectedListener());
GPRS.setOnItemSelectedListener(新的MyOnItemSelectedListener());
已选择公共视图(AdapterView父视图、视图、,
最终整数位置,长id){
//已选择一个项目。您可以使用
//parent.getItemAtPosition(位置)
SetDefaultAPN(apn_id[位置]);
Toast.makeText(parent.getContext(),“ETracking系统选择”+
parent.getItemAtPosition(position.toString(),Toast.LENGTH_LONG.show();
}
未选择公共无效(AdapterView父级){
//另一个接口回调
}
公共布尔值SetDefaultAPN(整数id)
{
布尔res=false;
ContentResolver解析器=配置。this.getContentResolver();
ContentValues=新的ContentValues();
值。put(“apn_id”,id);
尝试
{
解析程序更新(Uri.parse(“content://telephony/carriers/preferapn)、值、null、null);
游标c=resolver.query(
解析content://telephony/carriers/preferapn"),
无效的
“_id=“+id,
无效的
无效);
如果(c!=null)
{
res=真;
c、 close();
}
}
捕获(SQLE异常)
{
//Log.d(“TAG”,例如getMessage());
}
返回res;
}
/*
*枚举所有APN数据
*/
私有void枚举apns()
{
游标c=this.getContentResolver().query(
解析content://telephony/carriers/current)、空、空、空、空);
如果(c!=null)
{
//String s=“所有APN:\n”;
//Log.d(“标签”,s);
尝试
{
printAllData(c);//打印整个结果集
}
捕获(SQLE异常)
{
Toast.makeText(Configuration.this,“没有可用的网络连接”,Toast.LENGTH_LONG.show();
}
c、 close();
}
}
/*
*打印与游标c关联的所有数据记录。
*返回包含所有记录数据的字符串。
*出于某种奇怪的原因,Android SDK日志类无法打印很长的字符串消息。
*因此,我们必须一个记录一个记录地记录。
*/
私有void printaldata(光标c)
{
//如果(c==null)返回null;
if(c.moveToFirst())
{
name_of_GPRS_uavailable=新字符串[c.getCount()];
apn_id=newint[c.getCount()];
int i=0;
做{
可用的名称[i]=c.getString(c.getColumnIndex(“名称”);
apn_id[i]=c.getInt(c.getColumnIndex(“_id”);
//Log.d(“标签”,名称[i]);
i++;
}而(c.moveToNext());
//日志d(“标签”、“记录结束”);
//可用GPRS的名称[1]=“GPRS”;
}
}
请指导我。我该怎么做。我将非常感谢您我假设这是针对姜饼设备的,因为设置默认APN在ICS(4.0-API 14)中被删除,不推荐使用 您是否尝试添加:
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
您需要将这些权限添加到Android清单中:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
您还可以使用另一个微调器
让用户使用扫描结果从可用的Wifi点进行选择:
然后
WifiManager:
如果没有默认连接,则设置所需的网络
Markana提供了一个很好的使用Wifi的教程: