【问题标题】:How to add Wi-Fi option in GPRS spinner如何在 GPRS 微调器中添加 Wi-Fi 选项
【发布时间】:2012-11-16 04:37:05
【问题描述】:

在我的应用程序中,我使用“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 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";

          }   
      }

请指导我。我该怎么做。我会非常感谢你

【问题讨论】:

    标签: java android android-layout android-intent


    【解决方案1】:

    我认为这是针对 Gingerbread 设备,因为设置默认 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>
    

    您还可以额外添加一个Spinner,让用户使用扫描结果从可用的 Wifi 热点中进行选择:http://developer.android.com/reference/android/net/wifi/ScanResult.html

    然后

    WifiManager:http://developer.android.com/reference/android/net/wifi/WifiManager.html

    在没有默认连接的情况下设置所需的网络。

    Markana 有一个很好的关于以这种方式使用 Wifi 的教程:http://marakana.com/forums/android/examples/40.html

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 2019-03-24
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多