Skip to content

Instantly share code, notes, and snippets.

@a-thomas
Last active October 13, 2015 20:47
Show Gist options
  • Save a-thomas/4253537 to your computer and use it in GitHub Desktop.
Save a-thomas/4253537 to your computer and use it in GitHub Desktop.
Formation Android 2)
public class InputStreamToString {
public static String convert(InputStream is) {
String line = "";
StringBuilder builder = new StringBuilder();
BufferedReader rd=new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
builder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
public class ParlezVousPowerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
// power connected
} else {
// power disconnected
}
}
}
<receiver
android:name=".ParlezVousPowerReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
private ParlezVousPowerReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter1 = new IntentFilter(Intent.ACTION_POWER_CONNECTED);
IntentFilter filter2 = new IntentFilter(
Intent.ACTION_POWER_DISCONNECTED);
receiver = new ParlezVousPowerReceiver();
registerReceiver(receiver, filter1);
registerReceiver(receiver, filter2);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
<com.excilys.parlezvous.ParlezVousView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
public class ParlezVousView extends SurfaceView {
Paint paint = new Paint();
public ParlezVousView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
// Dessiner ici !
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Gérer les actions ici !
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
// relâchement du doigt
break;
}
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Appui du doigt
break;
case MotionEvent.ACTION_UP:
// Relâchement du doigt
break;
case MotionEvent.ACTION_MOVE:
// Déplacement du doigt
break;
}
return true;
}
private EditText usernameField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameField = (EditText) findViewById(R.id.username_field);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment