// startService would invoke onStartCommand method in MyService class // In general, this method would create sub-thread to running service public int onStartCommand(Intent intent, int flags, int startId) { Runnable runnable = new Runnable() { @Override public void run() { Log.i(TAG, "generate random number running"); } }; Thread thread = new Thread(runnable); // execute linearly not multi-thread, would stoke program // thread.run(); // achieve multi-thread thread.start(); /* START_STICKY: automatically restart after being killed and calls onStartCommand again, it can accept the new task instead of continuing with the previous task START_NOT_STICKY: Do not restart after being killed, do not keep the startup state, can stop at any time START_REDELIVER_INTENT: If there is an unfinished Intent, it is restarted after being killed, and all intents are sent after the restart. After stopSelf releases the maintained Intent. */ return START_NOT_STICKY; }
// stopService public void onDestroy() { super.onDestroy(); }
... // define data source List<Item> itemList = new ArrayList<Item>(); Item alpha = new Item("Alpha",R.drawable.alpha, "https://en.wikipedia.org/wiki/Alpha","API 1"); itemList.add(alpha); // add and set adapter ItemAdapter adapter = new ItemAdapter(MainActivity.this,R.layout.item, itemList);
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Item info = itemList.get(position); Bundle bundle = new Bundle();
// there is a back button in ToolBar <androidx.appcompat.widget.Toolbar android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:id="@+id/toolbar" android:background="?colorPrimary" android:layout_width="fill_parent" android:layout_height="?actionBarSize" android:minHeight="?actionBarSize" app:popupTheme="?actionBarPopupTheme" app:navigationIcon="?homeAsUpIndicator" xmlns:app="http://schemas.android.com/apk/res-auto" />
MainActivity
1 2 3 4 5 6 7 8 9 10 11 12
... mToolbar = (androidx.appcompat.widget.Toolbar) findViewById(R.id.toolbar); // set support to this toolbar setSupportActionBar((Toolbar) mToolbar); // set click listener ((Toolbar) mToolbar).setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); ...
Intent intent = new Intent(MainActivity.this,ItemPage.class); // first way post // intent.putExtra("itemLink",info.getLink()); // second way post intent.putExtras(bundle); startActivity(intent); // if finish() cannot back again // finish();
get
1 2 3 4 5 6
// first way get // getIntent().getStringExtra("link"); // second way get Bundle b=getIntent().getExtras(); //获取Bundle的信息 String link = b.getString("itemLink");
Version Problem between AndroidX and android.support.v7.XXX