我的应用程序停止运行,我不明白为什么;我认为我的代码是正确的。它只是一个简单的CALL应用程序
Button b;
Context= mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.call);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0102030405"));
startActivity(call);
return;
}
}
}
);
}
答案 0 :(得分:0)
将上下文初始化为mContext = getApplicationContext();在onCreate()
中答案 1 :(得分:0)
如果未授予错误权限,则启动活动。在else部分中,一旦获得权限,写下要执行的代码。也将此作为上下文传递。
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CALL_PHONE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
}
} else {
Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0102030405"));
startActivity(call);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0102030405"));
startActivity(call);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
此外,将以下行添加到清单:
<uses-permission android:name="android.permission.CALL_PHONE"/>
答案 2 :(得分:0)
示例代码
public class MainActivity extends AppCompatActivity {
private EditText ed_call;
private Button btn_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_call = (EditText) findViewById(R.id.ed_call);
btn_call = (Button) findViewById(R.id.btn_call);
btn_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent callintent = new Intent(Intent.ACTION_CALL);
callintent.setData(Uri.parse("tel:" + ed_call.getText().toString()));
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
startActivity(callintent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical"
tools:context="com.example.actiondemo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ed_call"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call" />
</LinearLayout>
</LinearLayout>
这可以帮助你。