1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package simpleperf.demo.cpp_api; 18 19 import android.support.v7.app.AppCompatActivity; 20 import android.os.Bundle; 21 import android.widget.TextView; 22 23 public class MainActivity extends AppCompatActivity { 24 25 // Used to load the 'native-lib' library on application startup. 26 static { 27 System.loadLibrary("native-lib"); 28 } 29 30 TextView textView; 31 32 @Override onCreate(Bundle savedInstanceState)33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.activity_main); 36 textView = findViewById(R.id.textView); 37 runNativeCode(); 38 39 createUpdateViewThread(); 40 } 41 createUpdateViewThread()42 void createUpdateViewThread() { 43 new Thread(new Runnable() { 44 @Override 45 public void run() { 46 while (true) { 47 try { 48 Thread.sleep(1000); 49 } catch (InterruptedException e) {} 50 final long count = getBusyThreadCount(); 51 runOnUiThread(new Runnable() { 52 @Override 53 public void run() { 54 textView.setText("Count: " + count); 55 } 56 }); 57 } 58 } 59 }).start(); 60 } 61 62 /** 63 * A native method that is implemented by the 'native-lib' native library, 64 * which is packaged with this application. 65 */ runNativeCode()66 private native void runNativeCode(); getBusyThreadCount()67 private native long getBusyThreadCount(); 68 69 } 70