367
No
class MainActivity
package com.example.choi.mystudy20;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
final static String TAG="MainActivity";
//다른 노티와 분별할 수 있는 식별자 임의 코드로 정한다.
final static int NOTI_ID =50;
Button btnNoti;
NotificationManager notiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNoti=(Button)findViewById(R.id.bt_noti);
btnNoti.setOnClickListener(listener);
notiManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
private void notiStart(){
Intent intent =new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("str", "노티값 (value)");
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification notification=new Notification.Builder(MainActivity.this)
.setSmallIcon(R.drawable.d1)
.setTicker("Noti Message")
.setContentTitle("Noti 제목")
.setContentText("Noti 내용")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.build();
notiManager.notify(NOTI_ID, notification);
//두번째 액티비 가기 종료
}
View.OnClickListener listener=new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "onClick v.getId() : " +v.getId());
switch (v.getId()){
case R.id.bt_noti:
notiStart();
break;
}
}
};
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.choi.mystudy20.MainActivity">
<Button
android:text="Noti Start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:id="@+id/bt_noti"
android:textAllCaps="false"
android:background="@android:color/holo_green_dark"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@android:color/background_light" />
</RelativeLayout>
class SecondActivity
package com.example.choi.mystudy20;
import android.app.NotificationManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
NotificationManager notificationManager;
TextView tv01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
tv01=(TextView)findViewById(R.id.tv01);
Intent intent=getIntent();
String s =intent.getStringExtra("str");
tv01.setText(s);
//버튼
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SecondActivity.this, "노티 종료", Toast.LENGTH_SHORT).show();
notificationManager.cancel(MainActivity.NOTI_ID);
}
});
}
}
R.layout.activity_second
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
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.choi.mystudy20.SecondActivity">
<Button
android:text="노티 종료"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="103dp"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textStyle="normal|bold"
android:textAlignment="center"
android:background="@android:color/holo_blue_light"
android:textColor="@android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/tv01"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="18sp" />
</RelativeLayout>
댓글 ( 4)
댓글 남기기