下面列出了android.content.Intent#setComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(HomeActivity.TAG,
"MockPaymentActivity onCreate " + getIntent() + " " + savedInstanceState);
String flag = getIntent().getStringExtra(FLAG);
if (flag != null) {
// pay request
Log.d(HomeActivity.TAG, "MockPaymentActivity onCreate pay request");
Intent intent = new Intent();
intent.setComponent(new ComponentName(mPkg, mFullName));
startActivity(intent);
} else {
// WeChat result
Log.d(HomeActivity.TAG, "MockPaymentActivity onCreate WeChat result");
setResult(Activity.RESULT_OK, getIntent());
finish();
}
}
private void startHOTSPOT() {
new PrefManager(context).setBoolPref("APisON", true);
try {
ApManager apManager = new ApManager(context);
if (!apManager.configApState()) {
Intent intent_tether = new Intent(Intent.ACTION_MAIN, null);
intent_tether.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.TetherSettings");
intent_tether.setComponent(cn);
intent_tether.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent_tether);
}
} catch (Exception e) {
Log.e(LOG_TAG, "BootCompleteReceiver ApManager exception " + e.getMessage() + " " + e.getCause());
}
}
/**
* Called to initialize a newly created client instance with the invalidation service.
*/
void initialize() {
// Create an intent that can be used to fire listener events back to the
// provided listener service. Use setComponent and not setPackage/setClass so the
// intent is guaranteed to be valid even if the service is not in the same application
Intent eventIntent = new Intent(Event.LISTENER_INTENT);
ComponentName component = new ComponentName(context.getPackageName(), listenerClass.getName());
eventIntent.setComponent(component);
Request request = Request
.newBuilder(Action.CREATE)
.setClientKey(clientKey)
.setClientType(clientType)
.setAccount(account)
.setAuthType(authType)
.setIntent(eventIntent)
.build();
executeServiceRequest(request);
addReference();
}
@Override
public void registerSession() {
mAudioManager.registerMediaButtonEventReceiver(mClementineMediaButtonEventReceiver);
// Create the intent
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(mClementineMediaButtonEventReceiver);
PendingIntent mediaPendingIntent = PendingIntent
.getBroadcast(mContext,
0,
mediaButtonIntent,
0);
// Create the client
mRcClient = new android.media.RemoteControlClient(mediaPendingIntent);
if (App.Clementine.getState() == Clementine.State.PLAY) {
mRcClient.setPlaybackState(android.media.RemoteControlClient.PLAYSTATE_PLAYING);
} else {
mRcClient.setPlaybackState(android.media.RemoteControlClient.PLAYSTATE_PAUSED);
}
mRcClient.setTransportControlFlags(android.media.RemoteControlClient.FLAG_KEY_MEDIA_NEXT |
android.media.RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS |
android.media.RemoteControlClient.FLAG_KEY_MEDIA_PLAY |
android.media.RemoteControlClient.FLAG_KEY_MEDIA_PAUSE);
mAudioManager.registerRemoteControlClient(mRcClient);
}
/**
* Creates and returns an intent that is valid for use in creating a new invalidation client
* that will deliver events to the test listener.
*/
public static Intent getEventIntent(Context context) {
Intent eventIntent = new Intent(Event.LISTENER_INTENT);
ComponentName component = new ComponentName(context.getPackageName(),
InvalidationTestListener.class.getName());
eventIntent.setComponent(component);
return eventIntent;
}
public static void invoke(Intent realIntent, Activity thatActivity) {
String calling = realIntent.getStringExtra("_mmessage_appPackage");
if(calling == null||"".equals(calling)){
calling = thatActivity.getCallingPackage(); //因为我们不是微信 无法获取app包名
if("".equals(calling) || calling == null){ //如果两种方法都失败
Toast.makeText(thatActivity,"抱歉,这个APP不能被伪装",Toast.LENGTH_SHORT).show();
return;
}
}
String callBackClass = calling + ".wxapi.WXEntryActivity";
ComponentName callBackName = new ComponentName(calling,callBackClass);
Intent callBackIntent = new Intent();
callBackIntent.putExtra("wx_token_key","com.tencent.mm.openapi.token"); //奇怪的来自微信验证
String stringExtra = "辣鸡腾讯坑我钱财";
int intExtra = 587333634;
String stringExtra2 = "com.tencent.mm";//微信验证用
byte[] wechatSign = wechatSign(stringExtra,intExtra,stringExtra2);//微信验证
callBackIntent.putExtra("_mmessage_content",stringExtra); //消息内容?
callBackIntent.putExtra("_mmessage_sdkVersion",intExtra); //微信版本.暂时回复常数 未来可能从微信获取
callBackIntent.putExtra("_mmessage_appPackage",stringExtra2); //微信包名?
callBackIntent.putExtra("_mmessage_checksum",wechatSign); //checksum
callBackIntent.putExtra("_wxapi_command_type",realIntent.getIntExtra("_wxapi_command_type",0)); //命令类型。还给你
callBackIntent.putExtra("_wxapi_baseresp_errcode",0); //错误码/是否成功。0:成功
String tempStr;
if((tempStr = realIntent.getStringExtra("_wxapi_baseresp_transaction")) != null) //有的请求没有,很奇怪,可能是旧版分享sdk
callBackIntent.putExtra("_wxapi_baseresp_transaction",tempStr); //未知
else
callBackIntent.putExtra("_wxapi_baseresp_transaction","1#sep#4#sep#4884235#sep#1504783117034");
callBackIntent.setFlags(272629760);
try{
callBackIntent.setComponent(callBackName);
thatActivity.startActivity(callBackIntent);
}catch (Exception e){
Toast.makeText(thatActivity,"包"+calling+"没有会调活动项。",Toast.LENGTH_SHORT).show();
return;
}
//Toast.makeText(this,R.string.msg_wechat_share_success,Toast.LENGTH_SHORT).show();
return;
}
/**
* From: <a href="https://stackoverflow.com/a/12328282">https://stackoverflow.com/a/12328282</a>
*/
public static @Nullable LabeledIntent getLabelintent(@NonNull Context context, @NonNull Intent origIntent, int name, int drawable) {
PackageManager pm = context.getPackageManager();
ComponentName launchName = origIntent.resolveActivity(pm);
if (launchName != null) {
Intent resolved = new Intent();
resolved.setComponent(launchName);
resolved.setData(origIntent.getData());
return new LabeledIntent(resolved, context.getPackageName(), name, drawable);
}
return null;
}
public void setExact(final int type, final long time, final PendingIntent operation) {
Intent i = new Intent(ACTION_SETEXACT);
i.setPackage(HELPER_PKG);
i.setComponent(new ComponentName(HELPER_PKG, HELPER_RECEIVER));
i.putExtra("type", type);
i.putExtra("time", time);
i.putExtra("operation", operation);
context.sendBroadcast(i);
}
private Intent createServiceBindIntent() {
Intent intent = new Intent();
if (mCreationParams != null) {
mCreationParams.addIntentExtras(intent);
}
intent.setComponent(mServiceName);
return intent;
}
public int addIntentOptions(int groupId, int itemId, int order,
ComponentName caller, Intent[] specifics, Intent intent, int flags,
MenuItem[] outSpecificItems) {
PackageManager pm = mContext.getPackageManager();
final List<ResolveInfo> lri =
pm.queryIntentActivityOptions(caller, specifics, intent, 0);
final int N = lri != null ? lri.size() : 0;
if ((flags & FLAG_APPEND_TO_GROUP) == 0) {
removeGroup(groupId);
}
for (int i=0; i<N; i++) {
final ResolveInfo ri = lri.get(i);
Intent rintent = new Intent(
ri.specificIndex < 0 ? intent : specifics[ri.specificIndex]);
rintent.setComponent(new ComponentName(
ri.activityInfo.applicationInfo.packageName,
ri.activityInfo.name));
final MenuItem item = add(groupId, itemId, order, ri.loadLabel(pm))
.setIcon(ri.loadIcon(pm))
.setIntent(rintent);
if (outSpecificItems != null && ri.specificIndex >= 0) {
outSpecificItems[ri.specificIndex] = item;
}
}
return N;
}
protected void selectImage() {
File tempFile = null;
try {
tempFile = createImageFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCurrentPhotoPath = tempFile.getAbsolutePath();
Uri outputFileUri = Uri.fromFile(tempFile);
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
final Intent pickPhoto = new Intent();
pickPhoto.setType("image/*");
pickPhoto.setAction(Intent.ACTION_GET_CONTENT);
pickPhoto.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
final Intent chooserIntent = Intent.createChooser(pickPhoto, getResources().getString(R.string.selectGallery));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, IMAGE_REQUEST_CODE);
}
private void startCamera ()
{
if (controlsInforma)
{
Intent intentSuckers = new Intent(this, InformaService.class);
intentSuckers.setAction(InformaService.ACTION_START_SUCKERS);
startService(intentSuckers);
informaCam.ioService.startDCIMObserver(CameraActivity.this, parentId, cameraComponent);
LocalBroadcastManager.getInstance(this).registerReceiver(mPhotoReceiver,
new IntentFilter("new-media"));
}
if (cameraIntentFlag != null)
{
cameraIntent = new Intent(cameraIntentFlag);
cameraIntent.setComponent(cameraComponent);
startActivityForResult(cameraIntent, Codes.Routes.IMAGE_CAPTURE);
}
else
{
setContentView(R.layout.activity_informacam_running);
Button btnStop = (Button)findViewById(R.id.informacam_button);
btnStop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
finish();//on destroy will do the rest
}
});
}
}
public static void openOtherApp(Context context, String packageName) {
PackageInfo pi;
PackageManager pm = context.getPackageManager();
try {
pi = pm.getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null) {
// String packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
context.startActivity(intent);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
@Override
protected void onListItemClick(final ListView l, final View v, final int position, final long id) {
final ActivityInfo info = (ActivityInfo) l.getItemAtPosition(position);
final Intent intent = new Intent();
intent.setComponent(new ComponentName(this, info.name));
startActivity(intent);
}
public int addIntentOptions(int groupId, int itemId, int order,
ComponentName caller, Intent[] specifics, Intent intent, int flags,
MenuItem[] outSpecificItems) {
PackageManager pm = mContext.getPackageManager();
final List<ResolveInfo> lri =
pm.queryIntentActivityOptions(caller, specifics, intent, 0);
final int N = lri != null ? lri.size() : 0;
if ((flags & FLAG_APPEND_TO_GROUP) == 0) {
removeGroup(groupId);
}
for (int i = 0; i < N; i++) {
final ResolveInfo ri = lri.get(i);
Intent rintent = new Intent(
ri.specificIndex < 0 ? intent : specifics[ri.specificIndex]);
rintent.setComponent(new ComponentName(
ri.activityInfo.applicationInfo.packageName,
ri.activityInfo.name));
final MenuItem item = add(groupId, itemId, order, ri.loadLabel(pm))
.setIcon(ri.loadIcon(pm))
.setIntent(rintent);
if (outSpecificItems != null && ri.specificIndex >= 0) {
outSpecificItems[ri.specificIndex] = item;
}
}
return N;
}
@Override
public boolean handleMessage(Message msg) {
if (msg.what == LAUNCH_ACTIVITY) {
Object r = msg.obj;
try {
Intent intent = (Intent) FieldUtil.getField(r.getClass(), r, "intent");
Intent target = intent.getParcelableExtra(HookHelper.TARGET_INTENT);
intent.setComponent(target.getComponent());
} catch (Exception e) {
e.printStackTrace();
}
}
mHandler.handleMessage(msg);
return true;
}
private void setupMediaSession() {
ComponentName mediaButtonReceiverComponentName = new ComponentName(getApplicationContext(), MediaButtonIntentReceiver.class);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(mediaButtonReceiverComponentName);
PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
mediaSession = new MediaSessionCompat(this, getResources().getString(R.string.main_activity_name), mediaButtonReceiverComponentName, mediaButtonReceiverPendingIntent);
mediaSession.setCallback(new MediaSessionCompat.Callback() {
@Override
public void onPlay() {
play();
}
@Override
public void onPause() {
pause();
}
@Override
public void onSkipToNext() {
playNextSong(true);
}
@Override
public void onSkipToPrevious() {
back(true);
}
@Override
public void onStop() {
quit();
}
@Override
public void onSeekTo(long pos) {
seek((int) pos);
}
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
return MediaButtonIntentReceiver.handleIntent(MusicService.this, mediaButtonEvent);
}
});
mediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
| MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
private static Intent meizu(Context context) {
Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
intent.putExtra("packageName", context.getPackageName());
intent.setComponent(new ComponentName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity"));
return intent;
}
/**
* Create and return an Intent that can launch the voice search activity, perform a specific
* voice transcription, and forward the results to the searchable activity.
*
* @param baseIntent The voice app search intent to start from
* @return A completely-configured intent ready to send to the voice search activity
*/
private Intent createVoiceAppSearchIntent(Intent baseIntent, SearchableInfo searchable) {
ComponentName searchActivity = searchable.getSearchActivity();
// create the necessary intent to set up a search-and-forward operation
// in the voice search system. We have to keep the bundle separate,
// because it becomes immutable once it enters the PendingIntent
Intent queryIntent = new Intent(Intent.ACTION_SEARCH);
queryIntent.setComponent(searchActivity);
PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent,
PendingIntent.FLAG_ONE_SHOT);
// Now set up the bundle that will be inserted into the pending intent
// when it's time to do the search. We always build it here (even if empty)
// because the voice search activity will always need to insert "QUERY" into
// it anyway.
Bundle queryExtras = new Bundle();
// Now build the intent to launch the voice search. Add all necessary
// extras to launch the voice recognizer, and then all the necessary extras
// to forward the results to the searchable activity
Intent voiceIntent = new Intent(baseIntent);
// Add all of the configuration options supplied by the searchable's metadata
String languageModel = RecognizerIntent.LANGUAGE_MODEL_FREE_FORM;
String prompt = null;
String language = null;
int maxResults = 1;
Resources resources = getResources();
if (searchable.getVoiceLanguageModeId() != 0) {
languageModel = resources.getString(searchable.getVoiceLanguageModeId());
}
if (searchable.getVoicePromptTextId() != 0) {
prompt = resources.getString(searchable.getVoicePromptTextId());
}
if (searchable.getVoiceLanguageId() != 0) {
language = resources.getString(searchable.getVoiceLanguageId());
}
if (searchable.getVoiceMaxResults() != 0) {
maxResults = searchable.getVoiceMaxResults();
}
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);
voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults);
voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, searchActivity == null ? null
: searchActivity.flattenToShortString());
// Add the values that configure forwarding the results
voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending);
voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras);
return voiceIntent;
}
InputBindResult startInputInnerLocked() {
if (mCurMethodId == null) {
return mNoBinding;
}
if (!mSystemReady) {
// If the system is not yet ready, we shouldn't be running third
// party code.
return new InputBindResult(null, null, mCurMethodId, mCurSeq);
}
InputMethodInfo info = mMethodMap.get(mCurMethodId);
if (info == null) {
throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
}
unbindCurrentMethodLocked(false, true);
mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
mCurIntent.setComponent(info.getComponent());
mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
com.android.internal.R.string.input_method_binding_label);
mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
| Context.BIND_NOT_VISIBLE | Context.BIND_SHOWING_UI)) {
mLastBindTime = SystemClock.uptimeMillis();
mHaveConnection = true;
mCurId = info.getId();
mCurToken = new Binder();
try {
if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
mIWindowManager.addWindowToken(mCurToken,
WindowManager.LayoutParams.TYPE_INPUT_METHOD);
} catch (RemoteException e) {
}
return new InputBindResult(null, null, mCurId, mCurSeq);
} else {
mCurIntent = null;
Slog.w(TAG, "Failure connecting to input method service: "
+ mCurIntent);
}
return null;
}