jiukexin 2016-10-30 14:30 采纳率: 0%
浏览 1125

如何插入包文件让这个顺利调试通过?在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条回答 默认 最新

  • devmiao 2016-10-31 01:04
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上