在我的应用程序中,我有一个对话框,显示2个选项:Copy
和Dismiss
当用户点击Copy
时,我想在状态栏中收到通知。
public void showDialog(final String planet) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setIcon(R.drawable.ic_launcher);
dialogBuilder.setTitle("Here You Go!");
dialogBuilder.setMessage(planet);
dialogBuilder.setPositiveButton("Copy", new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public void onClick(DialogInterface dialog, int which) {
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
clipboard.setText(planet);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("",planet);
clipboard.setPrimaryClip(clip);
}
Toast.makeText(getApplicationContext(), "Copied!", Toast.LENGTH_SHORT).show();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(List.this, List.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
}
这是代码,我在PendingIntent.getActivity
上收到错误,通知The method getActivity(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (new DialogInterface.OnClickListener(){}, int, Intent, int)
快速修复无济于事!
答案 0 :(得分:3)
使用WhateverYourActivityIsThatIsCallingYourShowDialogMethod.this
代替this
,其中WhateverYourActivityIsThatIsCallingYourShowDialogMethod
是此showDialog()
方法所在活动的名称。