2015-09-04 131 views
1

㈡有一个代码,以从其他应用程序,代码工作以及打开计算器,但我更改包名来打开不同的应用程序无法正常工作机器人工作室不容打开其他应用

有谁知道我不得不改变

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

+1

我不确定确切的问题是什么,你是说'launchCall()'不工作?如果是这样,请展示它的功能。 –

+0

你已经提供了一切,但我们实际需要看到的代码位... –

+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";,它看起来不像我活动的类名。