2015-09-04 130 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";,它看起來不像我活動的類名。