Created
October 13, 2014 05:30
-
-
Save ssd863419/2287e057f805e798785c to your computer and use it in GitHub Desktop.
Android 開啟已安裝的應用程序, ACTION_APPLICATION_DETAILS_SETTINGS, Intent, startActivity, ArrayAdapter, Spinner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.administrator.test; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
/** | |
* Created by Administrator on 2014/10/12. | |
*/ | |
public class EX0624 extends Activity { | |
private Button mButton1, mButton2; | |
@Override | |
public void onCreate(Bundle savedInstatnceState) { | |
super.onCreate(savedInstatnceState); | |
setContentView(R.layout.ex0624); | |
mButton1 = (Button) findViewById(R.id.myButton1); | |
mButton2 = (Button) findViewById(R.id.myButton2); | |
mButton1.setOnClickListener(new Button.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
/* 開啟設置鬧鐘畫面 */ | |
Intent intent = new Intent(); | |
intent.setAction("android.intent.action.SET_ALARM"); // 這句有點問題,待查 | |
startActivity(intent); | |
} | |
}); | |
mButton2.setOnClickListener(new Button.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
/* 開啟settings畫面 */ | |
Intent intent = new Intent(EX0624.this, EX0624_settings.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Open Alarms" | |
android:id="@+id/myButton1" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Open Manage apps" | |
android:id="@+id/myButton2" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="" | |
android:id="@+id/myTextView" /> | |
<Spinner | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/mySpinner" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Open Detail" | |
android:id="@+id/myButtonDetail" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Open All" | |
android:id="@+id/myButtonAll" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.administrator.test; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.provider.Settings; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.Spinner; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by Administrator on 2014/10/12. | |
*/ | |
public class EX0624_settings extends Activity { | |
private Button mButtonDetail, mButtonAll; | |
private Spinner mSpinner; | |
private TextView mTextView; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.ex0624_setting); | |
mButtonDetail = (Button) findViewById(R.id.myButtonDetail); | |
mButtonAll = (Button) findViewById(R.id.myButtonAll); | |
mSpinner = (Spinner) findViewById(R.id.mySpinner); | |
mTextView = (TextView) findViewById(R.id.myTextView); | |
/* 取得所有的packageName */ | |
PackageManager pm = getPackageManager(); | |
List<PackageInfo> packinfo = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES); | |
List<String> packnames = new ArrayList<String>(); | |
for (int i = 0 ; i< packinfo.size() ; i++) { | |
packnames.add(packinfo.get(i).packageName); | |
} | |
/* 將所有的packageName 放置ArrayAdapter */ | |
ArrayAdapter adapter = new ArrayAdapter( | |
this, android.R.layout.simple_spinner_item, packnames); | |
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
mSpinner.setAdapter(adapter); | |
mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { | |
mTextView.setText(arg0.getSelectedItem().toString()); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> arg0) { | |
mTextView.setText(""); | |
Toast.makeText(EX0624_settings.this, "請選擇",Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
mButtonAll.setOnClickListener(new Button.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
/* 開啟所有程序的畫面 */ | |
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS); | |
startActivity(intent); | |
} | |
}); | |
mButtonDetail.setOnClickListener(new Button.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
String packagename = "" + mTextView.getText(); | |
if (! packagename.equals("")) { | |
/* 以packagename開啟畫面 */ | |
Intent intent = new Intent( | |
Settings.ACTION_APPLICATION_DETAILS_SETTINGS, | |
Uri.parse("package:" + packagename)); | |
startActivity(intent); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!