Context context = getApplicationContext(); int resId = context.getResources().getIdentifier("my_image", "drawable", context.getPackageName()); imageView.setImageResource(resId);
Context context = getApplicationContext(); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Title") .setMessage("Message") .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User clicked OK button } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); AlertDialog dialog = builder.create(); dialog.show();
Context context = getApplicationContext(); Intent intent = new Intent(context, MyService.class); context.startService(intent);This code creates an intent to start a service called "MyService". It then starts the service using the application context. Package Library: android.content.Intent