Skip to content

Instantly share code, notes, and snippets.

@tksmaru
Created August 6, 2015 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tksmaru/a26a329c7310336ba80e to your computer and use it in GitHub Desktop.
Save tksmaru/a26a329c7310336ba80e to your computer and use it in GitHub Desktop.
Android API levelを意識したNotificationの実装例
package com.example.maruyama.helloandroid5;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(Build.VERSION_CODES.LOLLIPOP);
TextView view = (TextView) this.findViewById(R.id.textView);
view.setTextColor(Color.CYAN);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Notification: Start ----------------------");
Context context = getApplicationContext();
Intent bootIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, bootIntent, 0);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle("test title");
builder.setContentText("content is this");
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.notification_template_icon_bg);
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setPriority(Notification.PRIORITY_MAX); // from 16
builder.setColor(Color.RED); // from 21
notification = builder.build();
} else {
// for Android API level 15
notification = builder.getNotification(); // 11 -> 15
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(7, notification);
System.out.println("Notification: End ----------------------");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment