我正在使用 android 4.1 API 创建一个 android 应用程序,但我遇到了问题。
我的代码编译似乎有问题,虽然它没有给我任何错误,但当我尝试在 Android 手机上运行它时它确实崩溃了。
package com.example.meetingmute;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.media.AudioManager;
public class MainActivity extends Activity {
Button button;
TextView text;
boolean ringeronoff;
AudioManager mAudioManager;
AlertDialog ad;
@Override
public void onCreate(Bundle savedInstanceState) {
Context context = getApplicationContext();
ContentResolver contentResolver = getApplicationContext().getContentResolver();
final Cursor cursor = contentResolver.query(Uri.parse("content://calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null, null, null);
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
}
ad = new AlertDialog.Builder(this).create();
read();
Cursor cur = null;
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
Uri uri = cr.insert(Events.CONTENT_URI, values);
//提交查询并返回一个 Cursor 对象。cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
text = (TextView) findViewById(R.id.textView1);
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(ringeronoff == true){
text.setText("Ringer is On");
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
ringeronoff = false;
}
else{
text.setText("Ringer is Off");
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
ringeronoff = true;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}