布局

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="helloworld.com.inspur.demo8">
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <uses-permission android:name="android.permission.VIBRATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AActivity"/>
    </application>

</manifest>

逻辑

package helloworld.com.inspur.demo8;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.preference.MultiSelectListPreference;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ScrollingTabContainerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.jar.Manifest;

public class MainActivity extends AppCompatActivity {
    private Button bt1,bt2;
    private NotificationManager notificationManager;
    private Notification notification;
    private static final int NOTIFACATION_ID=0x123;
  //  private Intent intent;
   private  Context context=MainActivity.this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1= (Button)findViewById(R.id.bt1);
        bt2=(Button)findViewById(R.id.bt2);
        notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        bt1.setOnClickListener(new View.OnClickListener() {
            Intent intent=new Intent(context,AActivity.class);
            PendingIntent pendingIntent= PendingIntent.getActivities(context,0,new Intent[]{intent},0);
            @Override
            public void onClick(View v) {
            notification=new Notification.Builder(context)
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setTicker("标题啊~~~~")
                    .setContentTitle("标题啊")
                    .setContentText("今天周五")
                    .setWhen(System.currentTimeMillis())
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setContentIntent(pendingIntent)
                     .build();
                notificationManager.notify(NOTIFACATION_ID,notification);
            }
        });


        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notificationManager.cancel(NOTIFACATION_ID);
            }
        });




    }
}

 

配置

<?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: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:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="helloworld.com.inspur.demo8.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:text="设置"/>]

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt2"
        android:text="取消"/>

</LinearLayout>

 

跳转页面

package helloworld.com.inspur.demo8;


import android.app.Activity;
import android.os.Bundle;

public class AActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

 

posted on 2018-05-11 10:19  song.yan  阅读(233)  评论(0编辑  收藏  举报