Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
ishitcno1 / DatePickerFragment.java
Last active December 20, 2015 09:40
android date picker fragment
private class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, dayOfMonth);
}
@ishitcno1
ishitcno1 / TimePickerFragment.java
Last active May 22, 2022 06:56
android time picker fragment
private class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hourOfDay, minute, true);
}
@ishitcno1
ishitcno1 / getSensorInfo.java
Last active December 26, 2015 00:29
Get vendors and names of all sensors of an android device.
private String getSensorInfo(SensorManager sensorManager) {
Iterator iterator = sensorManager.getSensorList(Sensor.TYPE_ALL).iterator();
Sensor sensor;
String s = "";
while (iterator.hasNext()) {
sensor = (Sensor)iterator.next();
s = s + sensor.getName() + " MADE BY " + sensor.getVendor() + "\n";
}
return s;
}
@ishitcno1
ishitcno1 / ShowAccelerometerValues.java
Last active December 27, 2015 01:29
Show accelerometer values of android device to help define motions.
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
@ishitcno1
ishitcno1 / MainActivity.java
Last active December 25, 2023 04:26
Detect android device screen on, screen off and user present, then to do something.
public class MainActivity extends Activity {
private ScreenStateReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
private boolean isMyServiceRunning() {
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo serviceInfo: activityManager.getRunningServices(Integer.MAX_VALUE)) {
if (ShakeLockService.class.getName().equals(serviceInfo.service.getClassName())) {
return true;
}
}
return false;
}
@ishitcno1
ishitcno1 / AdPreference.java
Created November 20, 2013 09:40
A Preference to show admob banner ads.
import android.app.Activity;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
@ishitcno1
ishitcno1 / AdPreference.java
Created November 20, 2013 12:30
A Preference to show amazon banner ads.
import android.app.Activity;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.amazon.device.ads.AdLayout;
import com.amazon.device.ads.AdRegistration;
@ishitcno1
ishitcno1 / AndroidManifest.xml
Created November 21, 2013 11:05
Implement android widget
<receiver android:name=".MyAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info" />
</receiver>
@ishitcno1
ishitcno1 / MainActivity.java
Last active December 29, 2015 17:39
Android use ListFragment and SimpleAdapter. Creating event callbacks to the activity.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements SampleFragment.OnArticleSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);