在我的应用程序中,我有一个对话框,显示两个选项: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
时出现错误。快速修复无济于事!
最佳答案
使用WhateverYourActivityIsThatIsCallingYourShowDialogMethod.this
代替this
,其中WhateverYourActivityIsThatIsCallingYourShowDialogMethod
是此showDialog()
方法所在的活动的名称。
关于android - PendingIntent不适用于参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27343304/