添加电话拨打功能
This commit is contained in:
parent
e5dfd5b975
commit
64a9712294
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Thu Feb 20 06:57:14 GMT 2025
|
#Thu Feb 20 09:27:14 GMT 2025
|
||||||
stageCount=0
|
stageCount=0
|
||||||
libraryProject=winboll-shared
|
libraryProject=winboll-shared
|
||||||
baseVersion=1.0
|
baseVersion=1.0
|
||||||
publishVersion=1.0.0
|
publishVersion=1.0.0
|
||||||
buildCount=98
|
buildCount=99
|
||||||
baseBetaVersion=1.0.1
|
baseBetaVersion=1.0.1
|
||||||
|
@ -91,6 +91,8 @@
|
|||||||
|
|
||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
|
<activity android:name="cc.winboll.studio.contacts.activities.CallActivity"/>
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
@ -1,7 +1,5 @@
|
|||||||
package cc.winboll.studio.contacts;
|
package cc.winboll.studio.contacts;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -26,6 +24,7 @@ import java.util.ArrayList;
|
|||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import cc.winboll.studio.contacts.activities.CallActivity;
|
||||||
|
|
||||||
final public class MainActivity extends AppCompatActivity implements IWinBollActivity, ViewPager.OnPageChangeListener, View.OnClickListener {
|
final public class MainActivity extends AppCompatActivity implements IWinBollActivity, ViewPager.OnPageChangeListener, View.OnClickListener {
|
||||||
|
|
||||||
@ -80,7 +79,7 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct
|
|||||||
// 以下正常创建主窗口
|
// 以下正常创建主窗口
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
// 初始化工具栏
|
// 初始化工具栏
|
||||||
mToolbar = findViewById(R.id.activitymainToolbar1);
|
mToolbar = findViewById(R.id.activitymainToolbar1);
|
||||||
setSupportActionBar(mToolbar);
|
setSupportActionBar(mToolbar);
|
||||||
@ -94,7 +93,7 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct
|
|||||||
initView();//调用初始化视图方法
|
initView();//调用初始化视图方法
|
||||||
//initPoint();//调用初始化导航原点的方法
|
//initPoint();//调用初始化导航原点的方法
|
||||||
viewPager.addOnPageChangeListener(this);//滑动事件
|
viewPager.addOnPageChangeListener(this);//滑动事件
|
||||||
|
|
||||||
ViewPager viewPager = findViewById(R.id.activitymainViewPager1);
|
ViewPager viewPager = findViewById(R.id.activitymainViewPager1);
|
||||||
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
|
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
|
||||||
viewPager.setAdapter(pagerAdapter);
|
viewPager.setAdapter(pagerAdapter);
|
||||||
@ -118,7 +117,7 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct
|
|||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//初始化view,即显示的图片
|
//初始化view,即显示的图片
|
||||||
void initView() {
|
void initView() {
|
||||||
@ -131,7 +130,7 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct
|
|||||||
//linearLayout = findViewById(R.id.activitymainLinearLayout1);
|
//linearLayout = findViewById(R.id.activitymainLinearLayout1);
|
||||||
//initPoint();//初始化页面下方的点
|
//initPoint();//初始化页面下方的点
|
||||||
viewPager.setOnPageChangeListener(this);
|
viewPager.setOnPageChangeListener(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//初始化所要显示的布局
|
//初始化所要显示的布局
|
||||||
@ -147,7 +146,7 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct
|
|||||||
views.add(view2);
|
views.add(view2);
|
||||||
views.add(view3);
|
views.add(view3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// void initPoint() {
|
// void initPoint() {
|
||||||
// imageViews = new ImageView[5];//实例化5个图片
|
// imageViews = new ImageView[5];//实例化5个图片
|
||||||
// for (int i = 0; i < linearLayout.getChildCount(); i++) {
|
// for (int i = 0; i < linearLayout.getChildCount(); i++) {
|
||||||
@ -159,7 +158,7 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct
|
|||||||
// currentPoint = 0;//默认第一个坐标
|
// currentPoint = 0;//默认第一个坐标
|
||||||
// imageViews[currentPoint].setImageResource(R.drawable.ic_launcher);
|
// imageViews[currentPoint].setImageResource(R.drawable.ic_launcher);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//OnPageChangeListener接口要实现的三个方法
|
//OnPageChangeListener接口要实现的三个方法
|
||||||
/* onPageScrollStateChanged(int state)
|
/* onPageScrollStateChanged(int state)
|
||||||
此方法是在状态改变的时候调用,其中state这个参数有三种状态:
|
此方法是在状态改变的时候调用,其中state这个参数有三种状态:
|
||||||
@ -308,8 +307,11 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
// if (item.getItemId() == R.id.item_unittest) {
|
if (item.getItemId() == R.id.item_call) {
|
||||||
// WinBollActivityManager.getInstance(this).startWinBollActivity(this, UnitTestActivity.class);
|
Intent intent = new Intent(this, CallActivity.class);
|
||||||
|
startActivity(intent);
|
||||||
|
//WinBollActivityManager.getInstance(this).startWinBollActivity(this, CallActivity.class);
|
||||||
|
}
|
||||||
// } else
|
// } else
|
||||||
// if (item.getItemId() == R.id.item_exit) {
|
// if (item.getItemId() == R.id.item_exit) {
|
||||||
// exit();
|
// exit();
|
||||||
|
@ -0,0 +1,118 @@
|
|||||||
|
package cc.winboll.studio.contacts.activities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen@AliYun.Com
|
||||||
|
* @Date 2025/02/20 17:15:46
|
||||||
|
* @Describe 拨号窗口
|
||||||
|
*/
|
||||||
|
import android.Manifest;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.telephony.PhoneStateListener;
|
||||||
|
import android.telephony.TelephonyManager;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
import cc.winboll.studio.contacts.MainActivity;
|
||||||
|
import cc.winboll.studio.contacts.R;
|
||||||
|
|
||||||
|
public class CallActivity extends AppCompatActivity {
|
||||||
|
public static final String TAG = "CallActivity";
|
||||||
|
|
||||||
|
private static final int REQUEST_CALL_PHONE = 1;
|
||||||
|
private EditText phoneNumberEditText;
|
||||||
|
private TextView callStatusTextView;
|
||||||
|
private TelephonyManager telephonyManager;
|
||||||
|
private MyPhoneStateListener phoneStateListener;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
//setContentView(R.layout.activity_main);
|
||||||
|
setContentView(R.layout.activity_call);
|
||||||
|
|
||||||
|
phoneNumberEditText = findViewById(R.id.phone_number);
|
||||||
|
Button dialButton = findViewById(R.id.dial_button);
|
||||||
|
callStatusTextView = findViewById(R.id.call_status);
|
||||||
|
|
||||||
|
dialButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
String phoneNumber = phoneNumberEditText.getText().toString().trim();
|
||||||
|
if (!phoneNumber.isEmpty()) {
|
||||||
|
if (ContextCompat.checkSelfPermission(CallActivity.this, Manifest.permission.CALL_PHONE)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
ActivityCompat.requestPermissions(CallActivity.this,
|
||||||
|
new String[]{Manifest.permission.CALL_PHONE},
|
||||||
|
REQUEST_CALL_PHONE);
|
||||||
|
} else {
|
||||||
|
dialPhoneNumber(phoneNumber);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Toast.makeText(CallActivity.this, "请输入电话号码", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 初始化TelephonyManager和PhoneStateListener
|
||||||
|
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
|
||||||
|
phoneStateListener = new MyPhoneStateListener();
|
||||||
|
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
|
if (requestCode == REQUEST_CALL_PHONE) {
|
||||||
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
String phoneNumber = phoneNumberEditText.getText().toString().trim();
|
||||||
|
dialPhoneNumber(phoneNumber);
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, "未授予拨打电话权限", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dialPhoneNumber(String phoneNumber) {
|
||||||
|
Intent intent = new Intent(Intent.ACTION_CALL);
|
||||||
|
intent.setData(android.net.Uri.parse("tel:" + phoneNumber));
|
||||||
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
startActivity(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MyPhoneStateListener extends PhoneStateListener {
|
||||||
|
@Override
|
||||||
|
public void onCallStateChanged(int state, String incomingNumber) {
|
||||||
|
switch (state) {
|
||||||
|
case TelephonyManager.CALL_STATE_IDLE:
|
||||||
|
callStatusTextView.setText("电话已挂断");
|
||||||
|
break;
|
||||||
|
case TelephonyManager.CALL_STATE_OFFHOOK:
|
||||||
|
callStatusTextView.setText("正在通话中");
|
||||||
|
break;
|
||||||
|
case TelephonyManager.CALL_STATE_RINGING:
|
||||||
|
callStatusTextView.setText("来电: " + incomingNumber);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
// 取消监听
|
||||||
|
if (telephonyManager != null) {
|
||||||
|
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
28
contacts/src/main/res/layout/activity_call.xml
Normal file
28
contacts/src/main/res/layout/activity_call.xml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:padding="16dp">
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/phone_number"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:hint="请输入电话号码"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/dial_button"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="拨号"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/call_status"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingTop="16dp"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
@ -2,4 +2,8 @@
|
|||||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/item_call"
|
||||||
|
android:title="CallActivity"/>
|
||||||
|
|
||||||
</menu>
|
</menu>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Thu Feb 20 06:57:14 GMT 2025
|
#Thu Feb 20 09:27:14 GMT 2025
|
||||||
stageCount=0
|
stageCount=0
|
||||||
libraryProject=winboll-shared
|
libraryProject=winboll-shared
|
||||||
baseVersion=1.0
|
baseVersion=1.0
|
||||||
publishVersion=1.0.0
|
publishVersion=1.0.0
|
||||||
buildCount=98
|
buildCount=99
|
||||||
baseBetaVersion=1.0.1
|
baseBetaVersion=1.0.1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user