2012年12月30日 星期日

Turn On / off 3G Data


ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataMtd
.setAccessible(true);
dataMtd
.invoke(mgr, true/false);
you need android.permission.CHANGE_NETWORK_STATE permission too
share|improve this answer
Hi, Thanks. The only issue is that it cannot stably enable data connection in my HTC EVO 4G -- although the widget seems enabled but it is not really data connected. (Disabling data connection is always okay) – Richard Ye Dec 5 '11 at 7:03
feedback
You also need to add
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
to the manifest.xml
share|improve this answer
feedback
In your androidmanifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

2012年12月28日 星期五

Turn ON/OFF the Auto Sync Code in Java for Android This Code works from Android API Level 5 - private void turnAutoSyncOff(){ ContentResolver.setMasterSyncAutomatically(false); } private void turnAutoSyncOn(){ ContentResolver.setMasterSyncAutomatically(true); } private boolean isAutoSync() { return ContentResolver.getMasterSyncAutomatically(); } Posted by G-tendra at 1:28 PM Email This BlogThis! Share to Twitter Share to Facebook Labels: Android, Sync 2 comments: AnonymousMay 21, 2012 10:31 PM You forgot on permission android.permission.WRITE_SYNC_SETTINGS permission android.permission.READ_SYNC_SETTINGS in your Manifest.xml


Turn ON/OFF the Auto Sync Code in Java for Android

This Code works from Android API Level 5 -


private void turnAutoSyncOff(){
ContentResolver.setMasterSyncAutomatically(false);
}

private void turnAutoSyncOn(){
ContentResolver.setMasterSyncAutomatically(true);
}

private boolean isAutoSync() {
return ContentResolver.getMasterSyncAutomatically();
}

2 comments:

  1. You forgot on
    permission android.permission.WRITE_SYNC_SETTINGS
    permission android.permission.READ_SYNC_SETTINGS

    in your Manifest.xml

Auto rotate


public static void setAutoOrientationEnabled(ContentResolver resolver,
boolean enabled) {
Settings.System.putInt(resolver,
Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}

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


11down voteaccepted
You need the following permissions in your manifest file:
<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>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
Then you can use the following in your activity class:
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
wifiManager
.setWifiEnabled(true);
wifiManager
.setWifiEnabled(false);
Use the following to check if it's enabled or not
boolean wifiEnabled = wifiManager.isWifiEnabled()
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.
share|improve this answer
Related Posts Plugin for WordPress, Blogger...