zoukankan      html  css  js  c++  java
  • 第二阶段冲刺第四天

    public class MainActivity extends Activity {
    
     
    
        @Override
    
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
     
    
            Intent intent = new Intent(this, AutoReceiver.class);
    
            intent.setAction("VIDEO_TIMER");
    
                    // PendingIntent这个类用于处理即将发生的事情               
    
                    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
    
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    
                    // AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用相对时间
    
                    // SystemClock.elapsedRealtime()表示手机开始到现在经过的时间
    
                    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    
                    SystemClock.elapsedRealtime(), 10 * 1000, sender);
    
        }
    
    }
    public class AutoReceiver extends BroadcastReceiver {
    
        private static final int NOTIFICATION_FLAG = 1;
    
     
    
        @SuppressLint("NewApi")
    
        @Override
    
        public void onReceive(Context context, Intent intent) {
    
            if (intent.getAction().equals("VIDEO_TIMER")) {            
    
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
    
                        new Intent(context, MainActivity.class), 0);
    
                // 通过Notification.Builder来创建通知,注意API Level
    
                // API16之后才支持
    
                Notification notify = new Notification.Builder(context)
    
                        .setSmallIcon(R.drawable.ic_launcher)
    
                        .setTicker("TickerText:" + "您有新短消息,请注意查收!")
    
                        .setContentTitle("Notification Title")
    
                        .setContentText("This is the notification message")
    
                        .setContentIntent(pendingIntent).setNumber(1).build(); // 需要注意build()是在API
    
                                                       // level16及之后增加的,API11可以使用getNotificatin()来替代
    
                notify.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
    
                // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。
    
                NotificationManager manager = (NotificationManager) context
    
                        .getSystemService(Context.NOTIFICATION_SERVICE);
    
                manager.notify(NOTIFICATION_FLAG, notify);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示
    
            }
    
        }
    
     
    
    }
        <uses-sdk
    
            android:minSdkVersion="8"
    
            android:targetSdkVersion="19" />
    
    <receiver            android:name=".AutoReceiver"            android:label="@string/app_name" >        
        <intent-filter>          
          <action android:name="android.intent.action.BOOT_COMPLETED" />                 <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>    
        </receiver>

    这是可以代码但是出现了小问题,不能发送消息还需要改进;

  • 相关阅读:
    多路复用与设置阻塞、非阻塞模式
    ['\xef\xbb\xbf这个什么含义? PY技术开发交流区 乐讯手机高手
    fcntl使用 and_tt 博客园
    Linux 设备驱动 Edition 3Linux设备驱动第三版(中文版)
    CRT source Google 搜索
    BOM–字节序标记 永不放弃的地盘 博客频道 CSDN.NET
    在C语言中,unsigned char是什么类型?_百度知道
    The JR Concurrent Programming Language
    C语言:为什么用fprintf(stderr,"Error");比printf("Error");更好?
    bash
  • 原文地址:https://www.cnblogs.com/1234yyf/p/12976579.html
Copyright © 2011-2022 走看看