İçeriğe geç

Android Random Notification Örnek

Random üretilen bir sayının switch case ile kontrol edilip notification oluşturmasını sağlamak.

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.switchrandom.MainActivity" android:orientation="vertical">

<TextView android:text="Notification Example" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView" android:textSize="36sp" />

<Button android:id="@+id/buttonRandomNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="RANDOM NUMBER" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.buttonRandomNumber);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Random random = new Random();
int randomnumber;
randomnumber = random.nextInt(10)+5;
Toast.makeText(MainActivity.this,"Number is " + randomnumber,Toast.LENGTH_SHORT).show();
switch (randomnumber) {
case 10:
Toast.makeText(MainActivity.this, "case 10",Toast.LENGTH_SHORT).show();
addNotification(String.valueOf(randomnumber));
break;
case 11:
Toast.makeText(MainActivity.this, "case 11",Toast.LENGTH_SHORT).show();
addNotification(String.valueOf(randomnumber));
break;
case 12:
Toast.makeText(MainActivity.this,"case 12",Toast.LENGTH_SHORT).show();
addNotification(String.valueOf(randomnumber));
break;
case 13:
Toast.makeText(MainActivity.this, "case 13",Toast.LENGTH_SHORT).show();
addNotification(String.valueOf(randomnumber));
break;
default:
Toast.makeText(MainActivity.this, "case others ",Toast.LENGTH_SHORT).show();
break;
}
}
});
}
private void addNotification(String number) {
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Notifications")
.setContentText("This is case " + number);

Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NotificationManager.IMPORTANCE_HIGH, builder.build());
}
}
notification-example

notification-example-main



Tarih:Android

İlk Yorumu Siz Yapın

Bir Cevap Yazın