2012年12月26日 星期三
gps on off
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.view.Menu;
-----------------------------------------------------------
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
private void turnGPSOff(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
----------------------------------------------------------------
toggle
2012年12月23日 星期日
Enable bluetooth:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Enable bluetooth:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
<uses-permission android:name="android.permission.BLUETOOTH" />if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
} else {
mBluetoothAdapter.disable();
}
------------------------------------------------------------------------------------------
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
set wifi
You need the following permissions in your manifest file: Then you can use the following in your activity class: Use the following to check if it's enabled or not You'll find a nice tutorial on the subject on this site. wifi toggle power toggle setting |
2012年12月16日 星期日
android之手机震动Vibrate
2011-03-12 20:25 2122人阅读 评论(0) 收藏 举报 [java:nogutter] view plaincopy package cn.com.chenzheng_java; import android.app.Activity; import android.media.AudioManager; import android.os.Bundle; import android.os.Vibrator; import android.view.View; import android.widget.Toast; import android.widget.ToggleButton; public class VibrateActivity extends Activity { private ToggleButton button ; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.vibrate); button = (ToggleButton) findViewById(R.id.toggleButton1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Boolean flag = button.isChecked(); if(flag){ // 获取Vibrate对象 Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); // vibrator.vibrate(3000); // 设置Vibrate的震动周期 vibrator.vibrate(new long[]{1000,2000,3000,4000}, 0); Toast.makeText(VibrateActivity.this, "震动了", Toast.LENGTH_LONG).show(); } else{ } } }); } } android.os.Vibrate代表着手机的振动器。用法很简单,通过getSystemService()方法获取示例,然后调用vibrate方法便可以实现震动。 要使用该振动器必须在AndroidManifest.xml中指定权限
repeat play sound
setContentView(R.layout.activity_main); // Button "button1" Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // To-Do //ShowMsgDialog("哈哈哈"); //sendSMS("60399460", "message"); Thread thread = new Thread(){ @Override public void run(){ try{ playsound(); Thread.sleep(1000); run(); }catch (Exception e){ e.printStackTrace(); }finally{ } } }; //開始執行執行緒 thread.start(); } }); // End of Button "button1" } protected void playsound() { // TODO Auto-generated method stub MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.mysound); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub mp.release(); } }); mp.start(); }
play sound
mp = MediaPlayer.create(Test.this, R.raw.mysound);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
Send Message
This is works for me.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
sendSMS("5556", "Hi You got a message!");
/*here i can send message to emulator 5556. In Real device
you can change number */
}
});
}
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}You can add this line in AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>Take a look at this
This may helpful for you.
edited Jul 29 '12 at 3:59 |
訂閱:
文章 (Atom)