版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/78040358
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="online.geekgalaxy.ecall">
<!--permissions-->
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package online.geekgalaxy.ecall;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Vibrator;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Vibrator vibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* 想设置震动大小可以通过改变pattern来设定,如果开启时间太短,震动效果可能感觉不到
* */
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long [] pattern = {100,400,100,400}; // 停止 开启 停止 开启
vibrator.vibrate(pattern,2); //重复两次上面的pattern 如果只想震动一次,index设为-1
String number = "13XXXXXXXXX";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Toast.makeText(this, "No call permission!", Toast.LENGTH_SHORT).show();
return;
}
startActivity(intent);
}
public void onStop(){
super.onStop();
vibrator.cancel();
}
}
layout默认即可,虽然简单,但是写给家里的爸妈还是很实用的,***记得把权限都放开***
配个醒目的图标