如何插入包文件让这个顺利调试通过?在src根本无法新建包啊

在src下先建立包名为com.android.internal.telephony(右键src > new > package,create package-info.java打钩),然后右键刚建的com.android.internal.telephony(右键com.android.internal.telephony > new > File,文件名为ITelephony.aidl)

图片说明

android 自动拨打电话 挂断电话代码
页面布局文件代码 ( res下面的layout下面的activity_main.xml代码 )

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.u.phone.MainActivity" >

android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="28dp"
android:text="手机号码:" />

android:id="@+id/phonetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginTop="16dp"
android:text=""/>

android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="70dp"
android:text="呼叫几秒:" />

android:id="@+id/shijiantext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginTop="60dp"
android:text="5"/>

android:id="@+id/submitbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginLeft="30dp"
android:layout_marginTop="53dp"
android:text="拨打电话" />

android:id="@+id/clearcall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginLeft="160dp"
android:layout_marginTop="53dp"
android:text="停止呼叫" />

MainActivity.java代码 ( src下面的com.u.phone 下面的 MainActivity.java )

package com.u.phone;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
import com.android.internal.telephony.ITelephony;
import android.telephony.TelephonyManager;
import android.content.Context;
import java.lang.reflect.Method;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {
private Button start=null;
private Button stop =null;
private EditText photoNo=null;
private boolean runnable=true;
private boolean endCall=false;
private String telePhotoNo=null;
private EditText sjText=null;
private String sjNum=null;
ITelephony iPhoney=null;
//private TelephonyManager;
Thread t=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//取得资源
start=(Button)findViewById(R.id.submitbutton);
stop=(Button)findViewById(R.id.clearcall);
photoNo=(EditText)findViewById(R.id.phonetext);
sjText=(EditText)findViewById(R.id.shijiantext);
final TelephonyManager tm=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
iPhoney=getITelephony(this);//获取电话实例
//增加事件响应
start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
telePhotoNo=photoNo.getText().toString().trim();
sjNum=sjText.getText().toString().trim();
//System.out.println(telePhotoNo);
t.start();
}
});
//增加事件响应
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
runnable=false;
System.exit(0);
// finish();
}
});
//线程
t=new Thread(new Runnable() {

@Override
public void run() {
try {
while(runnable){

Thread.sleep(2000);//延时5s
int state=tm.getCallState();
if(state==TelephonyManager.CALL_STATE_IDLE){
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+telePhotoNo));
startActivity(intent);
}
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
Thread.sleep(Integer.parseInt(sjNum)*1000);//延时10s
endCall= iPhoney.endCall();
//System.out.println("是否成功挂断:"+endCall);
}

}
} catch (Exception e)
{
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.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**

  • 通过反射得到实例
  • @param context
  • @return */ private static ITelephony getITelephony(Context context) { TelephonyManager mTelephonyManager = (TelephonyManager) context .getSystemService(TELEPHONY_SERVICE); Class c = TelephonyManager.class; Method getITelephonyMethod = null; try { getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null); // 获取声明的方法 getITelephonyMethod.setAccessible(true); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } ITelephony iTelephony=null; try { iTelephony = (ITelephony) getITelephonyMethod.invoke( mTelephonyManager, (Object[]) null); // 获取实例 return iTelephony; } catch (Exception e) { e.printStackTrace(); } return iTelephony; }

}

电话权限: AndroidManifest.xml里加入下面2行权限代码


1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问