Android system have to Terminal application in default. This example provide user to run Linux command using ProcessBuilder, and display the result. But most Linux command not support in Android.
(If you want try the APK only, scroll down to the bottom.)
package com.example.androidrunlinuxcmd;
import java.io.IOException;
import java.io.InputStream;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
EditText cmdBox;
Button btnRun;
TextView textResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cmdBox = (EditText)findViewById(R.id.cmdbox);
btnRun = (Button)findViewById(R.id.run);
textResult = (TextView)findViewById(R.id.result);
textResult.setTypeface(Typeface.MONOSPACE);
btnRun.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//Split String from EditText to String Array
String[] cmd = cmdBox.getText().toString().split("\\s+");
try {
String cmdResult = runLinuxCmd(cmd);
textResult.setTextColor(Color.WHITE);
textResult.setText(cmdResult);
} catch (IOException e) {
e.printStackTrace();
textResult.setTextColor(Color.RED);
textResult.setText("Something Wrong!\n"
+ e.getMessage());
}
}});
}
//Run a Linux command and return result
private String runLinuxCmd(String[] command) throws IOException {
StringBuilder cmdReturn = new StringBuilder();
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
int c;
while ((c = inputStream.read()) != -1) {
cmdReturn.append((char) c);
}
return cmdReturn.toString();
}
}
This example read files "/sys/devices/system/cpu/cpu[0..]/cpufreq/scaling_cur_freq" with Linux's cat command, to determine running cpu frequency.
In Linux, there will be some file like this, to show the CPUs frequency.
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq
...
Run adb command:
$ adb shell ls /sys/devices/system/cpu/
It will show some directory named cpu0, cpu1..., it is the number of cpu in your system.
Run adb command, to show the current frequency of cpu0:
$ adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
By calling setCurrentItem() method of ViewPager, you can force it to scroll to any specified page. This example show how to scroll to the first and the last page.
package com.example.androidviewpagerapp;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
ViewPager viewPager;
MyPagerAdapter myPagerAdapter;
TextView textMsg;
Button btnToFirst, btnToLast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textMsg = (TextView)findViewById(R.id.msg);
textMsg.setMovementMethod(new ScrollingMovementMethod());
viewPager = (ViewPager) findViewById(R.id.myviewpager);
myPagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(myPagerAdapter);
viewPager.setOnPageChangeListener(myOnPageChangeListener);
btnToFirst = (Button)findViewById(R.id.tofirst);
btnToLast = (Button)findViewById(R.id.tolast);
btnToFirst.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
viewPager.setCurrentItem(0);
}});
btnToLast.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
int indexLast = viewPager.getAdapter().getCount() - 1;;
viewPager.setCurrentItem(indexLast);
}});
}
OnPageChangeListener myOnPageChangeListener =
new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int state) {
//Called when the scroll state changes.
}
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
//This method will be invoked when the current page is scrolled,
//either as part of a programmatically initiated smooth scroll
//or a user initiated touch scroll.
}
@Override
public void onPageSelected(int position) {
//This method will be invoked when a new page becomes selected.
textMsg.append("onPageSelected:" + position + "\n");
}};
private class MyPagerAdapter extends PagerAdapter {
int NumberOfPages = 5;
int[] res = { android.R.drawable.ic_dialog_alert,
android.R.drawable.ic_menu_camera,
android.R.drawable.ic_menu_compass,
android.R.drawable.ic_menu_directions,
android.R.drawable.ic_menu_gallery };
int[] backgroundcolor = { 0xFF101010, 0xFF202020, 0xFF303030,
0xFF404040, 0xFF505050 };
@Override
public int getCount() {
return NumberOfPages;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
textMsg.append("instantiateItem:" + position + "\n");
TextView textView = new TextView(MainActivity.this);
textView.setTextColor(Color.WHITE);
textView.setTextSize(30);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setText(String.valueOf(position));
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(res[position]);
LayoutParams imageParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(imageParams);
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setBackgroundColor(backgroundcolor[position]);
layout.setLayoutParams(layoutParams);
layout.addView(textView);
layout.addView(imageView);
final int page = position;
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"Page " + page + " clicked", Toast.LENGTH_LONG)
.show();
}
});
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
textMsg.append("destroyItem:" + position + "\n");
}
}
}
Refer to the former example of "Example of ViewPager with custom PagerAdapter": at start-up, instantiateItem() of MyPagerAdapter will be called twice, with position 0 and 1. When you you scroll page forward, extra instantiateItem() with position of the next invisible page will be called. And when you you scroll page backward, extra instantiateItem() with position of the former invisible page will be called.
So how can you know when page is scrolled, and which page is currently visible? One approach is to implement OnPageChangeListener for the ViewPager. The onPageSelected(int position) method will be called when a new page becomes selected. But not the first page at start-up.
Example:
package com.example.androidviewpagerapp;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
ViewPager viewPager;
MyPagerAdapter myPagerAdapter;
TextView textMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textMsg = (TextView)findViewById(R.id.msg);
textMsg.setMovementMethod(new ScrollingMovementMethod());
viewPager = (ViewPager) findViewById(R.id.myviewpager);
myPagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(myPagerAdapter);
viewPager.setOnPageChangeListener(myOnPageChangeListener);
}
OnPageChangeListener myOnPageChangeListener =
new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int state) {
//Called when the scroll state changes.
}
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
//This method will be invoked when the current page is scrolled,
//either as part of a programmatically initiated smooth scroll
//or a user initiated touch scroll.
}
@Override
public void onPageSelected(int position) {
//This method will be invoked when a new page becomes selected.
textMsg.append("onPageSelected:" + position + "\n");
}};
private class MyPagerAdapter extends PagerAdapter {
int NumberOfPages = 5;
int[] res = { android.R.drawable.ic_dialog_alert,
android.R.drawable.ic_menu_camera,
android.R.drawable.ic_menu_compass,
android.R.drawable.ic_menu_directions,
android.R.drawable.ic_menu_gallery };
int[] backgroundcolor = { 0xFF101010, 0xFF202020, 0xFF303030,
0xFF404040, 0xFF505050 };
@Override
public int getCount() {
return NumberOfPages;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
textMsg.append("instantiateItem:" + position + "\n");
TextView textView = new TextView(MainActivity.this);
textView.setTextColor(Color.WHITE);
textView.setTextSize(30);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setText(String.valueOf(position));
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(res[position]);
LayoutParams imageParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(imageParams);
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setBackgroundColor(backgroundcolor[position]);
layout.setLayoutParams(layoutParams);
layout.addView(textView);
layout.addView(imageView);
final int page = position;
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"Page " + page + " clicked", Toast.LENGTH_LONG)
.show();
}
});
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
textMsg.append("destroyItem:" + position + "\n");
}
}
}
This video show how to set up Google Play Services on Eclipse with Android SDK, reference Google document Setting Up Google Play Services.
Include how to:
- Install Google Play services SDK using Android SDK Manager.
- Import library project google-play-services_lib.
- Add referencing to google-play-services_lib in your project.
- Modify AndroidManifest.xml, to add <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> under <application>.
Also override onResume() method, to call GooglePlayServicesUtil.isGooglePlayServicesAvailable() method to test our setup.
Then finally, try something wrong, in case:
- With google-play-services_lib project closed, Errors of java.lang.NullPointerException occurred during the build.
- Without <meta-data...> in AndroidManifest.xml, no error in compiling, but fail in run time.
- Without referencing to google-play-services_lib, cannot resolve com.google
Once you've set up your project to reference the library project, you can begin developing features with the Google Play services APIs.
Write native iOS and Android applications with Xamarin.iOS and Xamarin.Android respectively
Learn strategies that allow you to share code between iOS and Android
Design user interfaces that can be shared across Android, iOS, and Windows Phone using Xamarin.Forms
Who This Book Is For
If you are a developer with experience in C# and are just getting into mobile development, this is the book for you. If you have experience with desktop applications or the Web, this book will give you a head start on cross-platform development.
In Detail
Developing a mobile application for just one platform is becoming a thing of the past. Companies expect their apps to be supported on both iOS and Android, while leveraging the best native features on both. Xamarin's tools help ease this problem by giving developers a single toolset to target both platforms.
This book is a step-by-step guide to building real-world applications for iOS and Android. The book walks you through building a chat application, complete with a backend web service and native features such as GPS location, camera, and push notifications. Additionally, you'll learn how to use external libraries with Xamarin and Xamarin.Forms to create shared user interfaces and make app-store-ready applications. This second edition has been updated with new screenshots and detailed steps to provide you with a holistic overview of the new features incorporated in Xamarin 3. By the end of the book, you will have gained expertise to build on the concepts learned and effectively develop a market-ready cross-platform application.
Written by an expert author team with years of hands-on experience in designing and building wearables, Professional Android Wearables covers how to use the Android Wear platform and other techniques to build real-world apps for a variety of wearables including smartbands, smartwatches, and smart glasses. In no time, you'll grasp how wearables can connect us to the Internet in more pervasive ways than with PCs, tablets, or mobile devices; how to build code using Google's Wear SDK for Android-enabled hardware devices; how Android Wear and other Android development techniques are capable of building several presented example projects; and much more.
Wearables are the next generation of smart mobile devices, it's no wonder you will want to master Android Wear SDK to build smart wearable apps for a multitude of form factors and applications.
Shows you how to navigate Android Wear SDK
Clearly explains how to use the Android Wear platform to build real-world apps
The companion website includes source code for all of the projects described in the book
If you're an experienced Android developer looking to master Android Wear SDK to build wearable apps, you've come to the right place.
Android is the fastest growing operating system (OS) with one of the largest installed bases of any mobile OS. Android uses one of the most popular programming languages, Java, as the primary language for building apps of all types. So, you should first obtain a solid grasp of the Java language and its foundation APIs to improve the chances of succeeding as an Android app developer.
This book will show you how to get your Android development environment set up and you will soon have your first working game. The difficulty level grows steadily with the introduction of key Java topics such as loops, methods, and OOP. You'll then use them in the development of games. You will learn how to build a math test game, a Simon-like memory game, a retro pong-style game, and for the grand finale, a Snake-style, retro arcade game with real Google Play leaderboards and achievements. The book has a hands-on approach and is packed with screenshots.
Learn the fundamental security models and motivations behind Linux, SELinux, and SE for Android.
Build and enable current security enhancements from the SE for Android project onto a working embedded UDOO board.
Discover how to leverage SE for Android to secure your own projects in powerful ways using this step by step guide.
Who This Book Is For
This book is intended for developers and engineers with some familiarity of operating system concepts as implemented by Linux. A basic background in C code would be helpful. Their positions range from hobbyists wanting to secure their Android powered creations to OEM engineers building handsets to engineers of emerging areas where Android is seeing growth.
In Detail
You will start by exploring the nature of the security mechanisms behind Linux and SELinux, and as you complete the chapters, you will integrate and enable SE for Android into a System on Chip (SoC), a process that, prior to this book, has never before been documented in its entirety! Discover Android's unique user space, from its use of the common UID and GID model to promote its security goals to its custom binder IPC mechanism. Explore the interface between the kernel and user space with respect to SELinux and investigate contexts and labels and their application to system objects.
This book will help you develop the necessary skills to evaluate and engineer secured products with the Android platform, whether you are new to world of Security Enhanced Linux (SELinux) or experienced in secure system deployment.
Last example show how to "Sharpen and blur bitmap using ScriptIntrinsicConvolve3x3", here is a interactive exercise of ScriptIntrinsicConvolve3x3. You can adjust coefficients of ScriptIntrinsicConvolve3x3, and view the result.