2013-06-18 40 views
1

那麼,我的應用程序會顯示WiFi信息(仍在工作進行中),並設置時間選擇器,當wifi打開。問題是我不知道如何連接AlarmManager方法中選擇器「set」對話框中的按鈕。這是密碼AlarmManager,TimePicker和Wifi控件。如何?

package com.pkg.androidmemoryinfo; 

import android.app.Activity; 
import android.app.ActivityManager; 
import android.app.ActivityManager.MemoryInfo; 
import android.app.Dialog; 
import android.app.TimePickerDialog; 
import android.content.Context; 
import android.net.wifi.WifiInfo; 
import android.net.wifi.WifiManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import android.widget.Toast; 

import java.util.Calendar; 

    public class MainActivity extends Activity implements OnClickListener { 

     private Button mTimeButton; 

     private Calendar mCalen; 
     private int hourOfDay; 
     private int minute; 
     private int ampm; 

     private static final int Time_PICKER_ID = 0; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      mTimeButton = (Button) findViewById(R.id.time_button); 
      mCalen = Calendar.getInstance(); 
      hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY); 
      minute = mCalen.get(Calendar.MINUTE); 
      ampm = mCalen.get(Calendar.AM_PM); 
      mTimeButton.setOnClickListener(this); 
     // Creating a memory_info Object 
      MemoryInfo memory_info = new MemoryInfo();  
      // Using Activity Manager System Service 
      ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
      activityManager.getMemoryInfo(memory_info);  
      // Assign Memory Value to free_memory Variable 
      long free_memory = memory_info.availMem/1048576L;  
      // Display free Ram Memory 
      TextView memoryInfoView = (TextView) findViewById(R.id.device_memory_status_txt); 
      memoryInfoView.setText("Free RAM Memory :-"+ free_memory + " MB");  


     } 
     public void run() { 
      WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
      WifiInfo info = wifi.getConnectionInfo(); 
      String ssid = info.getSSID(); 

      TextView ssidTextView = (TextView) findViewById(R.id.wifiSSID); 
      ssidTextView.setText(ssid); 
     } 
     @Override 
     @Deprecated 
     protected Dialog onCreateDialog(int id) { 

      switch (id) { 
       case Time_PICKER_ID: 
        return new TimePickerDialog(this, TimePickerListener, 
          hourOfDay, minute, false); 
      } 
      return null; 
     } 

     private TimePickerDialog.OnTimeSetListener TimePickerListener = 
       new TimePickerDialog.OnTimeSetListener() { 

        // while dialog box is closed, below method is called. 
        public void onTimeSet(TimePicker view, int hour, int minute) { 

         mCalen.set(Calendar.HOUR_OF_DAY, hour); 
         mCalen.set(Calendar.MINUTE, minute); 

         int hour12format = mCalen.get(Calendar.HOUR); 
         hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY); 
         minute = mCalen.get(Calendar.MINUTE); 
         ampm = mCalen.get(Calendar.AM_PM); 
         String ampmStr = (ampm == 0) ? "AM" : "PM"; 
         // Set the Time String in Button 

         TextView dateInfoView = (TextView) findViewById(R.id.dateinfo); 
         dateInfoView.setText("L'attivazione del Wi-Fi è impostata per le ore: " + hour12format + " : " + minute + "/" + ampmStr);  

         /// Here is the problem: Where have i to put this? /// 
         WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
         wifiManager.setWifiEnabled(true); 


        } 
       }; 

     @SuppressWarnings("deprecation") 
     @Override 
     public void onClick(View v) { 
      showDialog(Time_PICKER_ID); 
     } 
    } 

正如你所看到的我不知道如何把wifi控制。我該怎麼做?我如何用我的代碼創建一個AlarmManager?我需要一個例子如果可能的話。謝謝

回答

2

這是我認爲你可以使用的時間選擇器的一個例子。

--------主要活動-------

package com.example.chxboxtest; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity implements OnClickListener { 

private Button start_intent_button; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    start_intent_button = (Button) findViewById(R.id.start_intent_button); 
    start_intent_button.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 

    case R.id.start_intent_button: { 

     Intent intent = new Intent(this,TimePickerTest.class); 
     startActivity(intent); 

    } 
     break; 
    } 

} 

} 

-------------主要活動XML ------ -----------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<Button 
    android:id="@+id/start_intent_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Start Intent Time Picker" 
    android:layout_centerInParent="true" 
    /> 


</RelativeLayout> 

------------- TimePicker類---------------

package com.example.chxboxtest; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import android.widget.Toast; 


public class TimePickerTest extends Activity implements OnClickListener{ 

private CheckBox cBox; 
private TimePicker tPicker; 
private TextView showTime; 
private Button done; 

private String hour; 
private String minute; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.time_picker_layout); 

    initButtons(); 
} 



private void initButtons() { 

    tPicker = (TimePicker) findViewById(R.id.time_picker); 
    showTime = (TextView) findViewById(R.id.get_time); 
    done = (Button)findViewById(R.id.done); 
    cBox = (CheckBox) findViewById(R.id.time_picker_checkbox); 
    cBox.setOnClickListener(this); 
    done.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 

    //If check enable or disble timePicker 
    case R.id.time_picker_checkbox: { 

     if (((CheckBox) v).isChecked()) { 
      Toast.makeText(this, "CHECKED", Toast.LENGTH_SHORT).show(); 
      tPicker.setEnabled(false); 
     } else { 
      Toast.makeText(this, "NOT CHECKED", Toast.LENGTH_SHORT).show(); 
      tPicker.setEnabled(true); 
     } 
    } 
     break; 

//If Done button pressed get time selected by user 

    case R.id.done:{ 


     tPicker.clearFocus(); 
     // re-read the values, in my case i put them in a Time object. 
     hour = tPicker.getCurrentHour().toString(); 
     minute = tPicker.getCurrentMinute().toString(); 

     if(tPicker.getCurrentMinute().intValue() < 10){ 

      String setTimeText = hour+ " : " + "0" + minute; 
      showTime.setText(setTimeText); 
     }else{ 
      String setTimeText = hour+ " : " + minute; 
      showTime.setText(setTimeText); 
     } 





    } 
     break; 

    default: 
     break; 

    } 

} 

    } 

------------- TimerPicker XML ------------

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/timer_textview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/time_picker" 
    android:layout_toRightOf="@+id/time_picker_checkbox" 
    android:text="Check this to Cancel Alarm" /> 

<CheckBox 
    android:id="@+id/time_picker_checkbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/time_picker" 
    android:layout_centerHorizontal="true" /> 

<TimePicker 
    android:id="@+id/time_picker" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" /> 

<Button 
    android:id="@+id/done" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/time_picker" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:text="Done" /> 

<TextView 
    android:id="@+id/get_time" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="center_horizontal" 
    android:text="Time" 
    android:textColor="#FF0000" 
    android:textSize="25sp" /> 

</RelativeLayout> 

------------- -----------的Manifest.xml

 **<activity 
     android:name="com.example.chxboxtest.TimePickerTest" 
     android:configChanges="orientation|keyboardHidden"> 
    </activity>** 

不要忘記添加TimePicker類的活動。 BestPracice添加所有XML文本中的strings.xml

乾杯

+0

太感謝你了,你覺得可以很容易插入使用你的wifi的代碼? – Davidebj