有谁知道我要改变什么
package com.example.zeluis.c4;
import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class c4 extends AppCompatActivity {
// aki comesa abrir os app
private String packge_name = "com.android.calculator2";
private String class_name = "com.android.calculator2.Calculator";
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c4);
bt = (Button) findViewById(R.id.but);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
launchCall();
}
});
}
protected void launchCall() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packge_name, class_name));
try {
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_c4, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的想法是更改软件包名称以打开另一个应用程序,例如whatapp或Navfree .etc
答案 0 :(得分:0)
最简单的方法是向软件包管理器询问软件包的“启动意图”:
private static void launchPackage(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException ex) {
Toast.makeText(context, "Unknown package, or has no launching activity", Toast.LENGTH_SHORT).show();
}
}
这样您就不需要知道启动器活动类名称了。包管理器将在包的清单中查找。
但是,如果您检查它返回的意图,您将看到它与您的代码构建的内容基本相同。所以我怀疑你使用的是packageName和/或className的错误值。
您在评论中写道,您正在使用private String class_name = "com.navfree.android.OSM.ALL.com.navfree.android.OSM.ALL-2";
进行测试,该评论看起来不像我的活动的类名。